#include <bits/stdc++.h>
using namespace std;
unordered_map<long long, long long> m[61][61];
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 rx = (lx == 0 ? 0 : x % (1LL << lx));
long long ry = (ly == 0 ? 0 : y % (1LL << ly));
long long key = (rx << 32) | ry;
m[lx][ly][key] += v;
}
while(q--){
long long x, y;
cin >> x >> y;
long long res = 0;
for(int xx = 0; xx <= 60; xx++){
long long px = 1LL << xx;
if(px > x) break;
long long rx = x % px;
for(int yy = 0; yy <= 60; yy++){
long long py = 1LL << yy;
if(py > y) break;
long long ry = y % py;
long long key = (rx << 32) | ry;
res += m[xx][yy][key];
}
}
cout << res << '\n';
}
return 0;
}