#include <bits/stdc++.h>
using namespace std;
unordered_map<long long, long long> m[61][61];
long long make_key(long long x, long long y) {
return (x << 32) | y;
}
int main() {
ios::sync_with_stdio(false);
cin.tie(nullptr);
long long n, q;
cin >> n >> q;
for(long long i=0; i<n; i++){
long long x, y, v;
cin >> x >> y >> v;
int lx = x == 0 ? 0 : 63 - __builtin_clzll(x);
int ly = y == 0 ? 0 : 63 - __builtin_clzll(y);
long long key = make_key(x % (1LL << lx), y % (1LL << ly));
m[lx][ly][key] += v;
}
while(q--){
long long r, c;
cin >> r >> c;
long long res = 0;
for(int xx=0; xx<=60; xx++){
long long px = 1LL << xx;
if(px > r) break;
long long rx = r % px;
for(int yy=0; yy<=60; yy++){
long long py = 1LL << yy;
if(py > c) break;
long long ry = c % py;
long long key = make_key(rx, ry);
if(m[xx][yy].count(key))
res += m[xx][yy][key];
}
}
cout << res << "\n";
}
return 0;
}