#include <bits/stdc++.h>
using namespace std;
const int N=200010;
long long x[N],y[N],v[N],a[N],b[N];
int n,t;
long long lowbit(long long x)
{
return x&-x;
}
long long change(long long x)
{
long long cnt=0;
while (x>0)
{
cnt=x;
x-=lowbit(x);
}
return cnt;
}
int main() {
freopen("modern.in","r",stdin);
freopen("modern.out","w",stdout);
scanf("%d%d",&n,&t);
for (int i=1;i<=n;i++)
{
scanf("%lld%lld%lld",&x[i],&y[i],&v[i]);
a[i]=change(x[i]);
b[i]=change(y[i]);
}
while (t--)
{
int ret=0;
long long r,c;
scanf("%lld%lld",&r,&c);
for (int i=1;i<=n;i++)
{
if ((r-x[i])%a[i]==0 && (c-y[i])%b[i]==0)
{
ret+=v[i];
}
}
printf("%d\n",ret);
}
}