#include<bits/stdc++.h>
using namespace std;
//#define ll long long
#define MAXN 200005
struct node{
long long x,y;
};
long long s[MAXN],cl[MAXN];
long long x[MAXN],y[MAXN],v[MAXN];
long long r[MAXN],c[MAXN];
long long n,q;
std::map<node,long long > mapz;
long long qs(long long idx){
int l=0,r=60;
while(l<r){
int mid=(l+r+1)/2;
if(s[mid]>idx)r=mid-1;
else l=mid;
}
return s[l];
}
int main(){
freopen("modern.in","r",stdin);
freopen("modern.out","w",stdout);
s[0]=1;
for(int i=1;i<=60;i++){
s[i]=2*s[i-1];
}
cin>>n>>q;
for(int i=1;i<=n;i++)cin>>x[i]>>y[i]>>v[i];
for(int i=1;i<=q;i++)cin>>r[i]>>c[i];
for(int i=1;i<=n;i++){
int a=qs(x[i]),b=qs(y[i]);
// cout<<x[i]<<" "<<y[i]<<" "<<a<<" "<<b<<endl;
for(int j=1;j<=q;j++){
// if((r[j]%a==x[i]||(a==x[i]&&r[j]%a==0))&&(c[j]%b==y[i]||(b==y[i]&&c[j]%b==0)))cl[j]+=v[i];
// cout<<r[j]%a<<" "<<c[j]%b<<endl;
if((r[j]-x[i])%a==0&&(c[j]-y[i])%b==0)cl[j]+=v[i];
}
}
for(int i=1;i<=q;i++)cout<<cl[i]<<endl;
return 0;
}