#include <bits/stdc++.h>
using namespace std;
#define endl '\n'
using ll = long long;
using ull = unsigned long long;
unordered_map<ull, ll> m[61][61];
vector<pair<int, int>> active;
bool used[61][61];
ll pw[61];
inline ull make_key(ll rx, ll ry) {
return rx ^ (ry * 11995408973635179863ULL);
}
int main() {
ios::sync_with_stdio(false);
cin.tie(nullptr);
for (int i = 0; i <= 60; i++) {
pw[i] = (1LL << i);
}
for (int i = 0; i <= 60; i++) {
for (int j = 0; j <= 60; j++) {
m[i][j].reserve(256);
}
}
ll n, q;
cin >> n >> q;
ll x, y, v;
for (int i = 0; i < n; i++) {
cin >> x >> y >> v;
int lx = 63 - __builtin_clzll(x);
int ly = 63 - __builtin_clzll(y);
ll rx = x & (pw[lx] - 1);
ll ry = y & (pw[ly] - 1);
if (!used[lx][ly]) {
used[lx][ly] = true;
active.push_back({lx, ly});
}
ull key = make_key(rx, ry);
m[lx][ly][key] += v;
}
while (q--) {
cin >> x >> y;
ll res = 0;
for (auto [lx, ly] : active) {
if (pw[lx] > x || pw[ly] > y) continue;
ll rx = x & (pw[lx] - 1);
ll ry = y & (pw[ly] - 1);
ull key = make_key(rx, ry);
auto it = m[lx][ly].find(key);
if (it != m[lx][ly].end()) {
res += it->second;
}
}
cout << res << endl;
}
return 0;
}