#include<bits/stdc++.h>
using namespace std;
int N,Q;
long long X[2005],Y[2005],V[2005],A[2005],B[2005];
int main(){
freopen("modern.in","r",stdin);
freopen("modern.out","w",stdout);
cin>>N>>Q;
long long x,y,v;
for(int i=1;i<=N;i++){
cin>>x>>y>>v;
int a=pow(2,x/2),b=pow(2,y/2);
A[i]=a,B[i]=b,X[i]=x,Y[i]=y,V[i]=v;
}
long long r,c;
for(int i=1;i<=Q;i++){
cin>>r>>c;
long long sum=0;
for(int i=1;i<=N;i++){
if((r-X[i])%A[i]==0&&(c-Y[i])%B[i]==0){
sum+=V[i];
}
}
cout<<sum<<endl;
}
return 0;
}