#include <bits/stdc++.h>
const int N = 2e4 + 5;
const int INF = 0x3f3f3f3f;
using namespace std;
int n, p, c[N], h[N], t[N], m, f[N], ans[N], lim;
vector<pair<int, int> > vec1[N * 16], vec2[N * 16];
template <typename T>
inline T read() {
  T x = 0, w = 1;
  char c = getchar();
  while (c < '0' || c > '9') {
    if (c == '-') w = -1;
    c = getchar();
  }
  while (c >= '0' && c <= '9') x = x * 10 + c - '0', c = getchar();
  return x * w;
}
void insert(int p, int l, int r, int ql, int qr, int c, int v) {
  if (ql <= l && r <= qr) return (void)(vec1[p].push_back(make_pair(c, v)));
  int mid = (l + r) >> 1;
  if (ql <= mid) insert(p << 1, l, mid, ql, qr, c, v);
  if (mid < qr) insert(p << 1 | 1, mid + 1, r, ql, qr, c, v);
}
void modify(int p, int l, int r, int k, int sz, int x) {
  vec2[p].push_back(make_pair(x, sz));
  if (l == r) return;
  int mid = (l + r) >> 1;
  if (k <= mid)
    modify(p << 1, l, mid, k, sz, x);
  else
    modify(p << 1 | 1, mid + 1, r, k, sz, x);
}
void solve(int p, int l, int r) {
  int sz = vec1[p].size();
  for (int c, v, i = 0; i < sz; i++) {
    c = vec1[p][i].first, v = vec1[p][i].second;
    for (int j = 4000; j >= c; j--) f[j] = max(f[j], f[j - c] + v);
  }
  for (int i = 1; i <= 4000; i++) f[i] = max(f[i], f[i - 1]);
  sz = vec2[p].size();
  for (int tmp, i = 0; i < sz; i++)
    ans[tmp = vec2[p][i].first] = max(ans[tmp], f[vec2[p][i].second]);
  if (l == r) return;
  int mid = (l + r) >> 1, g[4000];
  for (int i = 0; i <= 4000; i++) g[i] = f[i];
  solve(p << 1, l, mid);
  for (int i = 0; i <= 4000; i++) f[i] = g[i];
  solve(p << 1 | 1, mid + 1, r);
  for (int i = 0; i <= 4000; i++) f[i] = g[i];
}
int main() {
  n = read<int>(), p = read<int>();
  for (int i = 1; i <= n; i++)
    c[i] = read<int>(), h[i] = read<int>(), t[i] = read<int>(),
    lim = max(lim, t[i]);
  lim = lim + p - 1;
  for (int i = 1; i <= n; i++)
    insert(1, 1, lim, t[i], t[i] + p - 1, c[i], h[i]);
  m = read<int>();
  for (int a, b, i = 1; i <= m; i++) {
    a = read<int>(), b = read<int>();
    if (a > lim) {
      ans[i] = 0;
      continue;
    }
    modify(1, 1, lim, a, b, i);
  }
  solve(1, 1, lim);
  for (int i = 1; i <= m; i++) printf("%d\n", ans[i]);
  return 0;
}
