#include <bits/stdc++.h>
using namespace std;
#define endl '\n'
unordered_map<long long, long long> m[61][61];
long long n, q, x, y, v, res;
int main()
{
ios_base::sync_with_stdio(0);cin.tie(0);cout.tie(0);
cin>>n>>q;
for(int i = 1 ; i<=n ; i++){
cin>>x>>y>>v;
int lx = 63 - __builtin_clzll(x);
int ly = 63 - __builtin_clzll(y);
long long key = (x%(1LL<<lx) << 32) | (y%(1LL<<ly));
m[lx][ly][key]+=v;
}
for(int i = 1 ; i<=q ; i++){
res = 0;
cin>>x>>y;
for(int xx = 0 ; xx<=60 ; xx++){
if((1LL << xx) >x)break;
for(int yy = 0 ; yy<=60 ; yy++){
if((1LL << xx) <=x && (1LL << yy)<=y){
long long key = (x%(1LL << xx) << 32) | (y%(1LL << yy));
if(m[xx][yy].count(key)){
res+=m[xx][yy][key];
}
}
else break;
}
}
cout<<res<<endl;
}
return 0;
}