#include <bits/stdc++.h>
using namespace std;
struct query {
  int t, v, id;
} a[100002];
vector<int> v[100002 * 4];
int n, p, q, m, T, i, j, c[100002], h[100002], t[100002], f[100002],
    ans[100002];
int read() {
  char c = getchar();
  int w = 0;
  while (c < '0' || c > '9') c = getchar();
  while (c <= '9' && c >= '0') {
    w = w * 10 + c - '0';
    c = getchar();
  }
  return w;
}
void change(int p, int l, int r, int ql, int qr, int x) {
  if (ql <= l && r <= qr) {
    v[p].push_back(x);
    return;
  }
  int mid = (l + r) / 2;
  if (ql <= mid) change(p * 2, l, mid, ql, qr, x);
  if (qr > mid) change(p * 2 + 1, mid + 1, r, ql, qr, x);
}
int my_comp(const query &x, const query &y) { return x.t < y.t; }
void dfs(int x, int l, int r) {
  int g[100002], mid = (l + r) / 2;
  for (int i = 0; i <= m; i++) g[i] = f[i];
  for (int i = 0; i < v[x].size(); i++) {
    for (int j = m; j >= c[v[x][i]]; j--)
      f[j] = max(f[j], f[j - c[v[x][i]]] + h[v[x][i]]);
  }
  if (l < r)
    dfs(x * 2, l, mid), dfs(x * 2 + 1, mid + 1, r);
  else {
    while (j <= q && a[j].t <= l) {
      ans[a[j].id] = f[a[j].v];
      j++;
    }
  }
  for (int i = 0; i <= m; i++) f[i] = g[i];
}
int main() {
  n = read();
  p = read();
  for (i = 1; i <= n; i++) {
    c[i] = read(), h[i] = read(), t[i] = read();
    T = max(T, t[i] + p - 1);
  }
  for (i = 1; i <= n; i++) change(1, 1, T, t[i], t[i] + p - 1, i);
  q = read();
  for (i = 1; i <= q; i++) {
    a[i].t = read(), a[i].v = read();
    a[i].id = i;
    m = max(m, a[i].v);
  }
  sort(a + 1, a + q + 1, my_comp);
  j = 1;
  dfs(1, 1, T);
  for (i = 1; i <= q; i++) printf("%d\n", ans[i]);
  return 0;
}
