#include <bits/stdc++.h>
using namespace std;
const int inf = 2e9;
const int MOD = 1e9 + 7;
const int M = 4e3, N = M + 10;
pair<int, pair<int, int> > a[N], b[20010];
long long f[2][N][N] = {0}, cur[N], ans[20010];
int n, T, m;
void prepare() {
  scanf("%d%d", &n, &T);
  for (int i = 1; i <= n; i++)
    scanf("%d%d%d", &a[i].second.first, &a[i].second.second, &a[i].first);
  scanf("%d", &m);
  for (int i = 1; i <= m; i++) {
    scanf("%d%d", &b[i].first, &b[i].second.first);
    b[i].second.second = i;
  }
  sort(a + 1, a + n + 1);
  sort(b + 1, b + m + 1);
}
void solve() {
  if (0) {
    for (int i = 1; i <= n; i++)
      printf("%d %d %d\n", a[i].first, a[i].second.first, a[i].second.second);
    for (int i = 1; i <= m; i++)
      printf("%d %d %d\n", b[i].first, b[i].second.first, b[i].second.second);
  }
  int top1 = 0, top2 = 0, L = 1, R = 0;
  for (int i = 1; i <= m; i++) {
    while (L <= R && top1 && a[L].first + T <= b[i].first) top1--, L++;
    while (R < n && a[R + 1].first <= b[i].first) {
      R++;
      top2++;
      for (int j = M; j >= 0; j--) {
        f[1][top2][j] = f[1][top2 - 1][j];
        if (j >= a[R].second.first)
          f[1][top2][j] =
              max(f[1][top2][j],
                  f[1][top2 - 1][j - a[R].second.first] + a[R].second.second);
      }
      for (int j = 1; j <= M; j++)
        f[1][top2][j] = max(f[1][top2][j], f[1][top2][j - 1]);
    }
    if (!top1) {
      top2 = 0;
      for (int x = R; x >= L; x--) {
        top1++;
        for (int j = M; j >= 0; j--) {
          f[0][top1][j] = f[0][top1 - 1][j];
          if (j >= a[x].second.first)
            f[0][top1][j] =
                max(f[0][top1][j],
                    f[0][top1 - 1][j - a[x].second.first] + a[x].second.second);
        }
      }
      for (int j = 1; j <= M; j++)
        f[0][top1][j] = max(f[0][top1][j], f[0][top1][j - 1]);
    }
    while (L <= R && top1 && a[L].first + T <= b[i].first) top1--, L++;
    for (int j = 0; j <= b[i].second.first; j++)
      ans[b[i].second.second] =
          max(ans[b[i].second.second],
              f[0][top1][j] + f[1][top2][b[i].second.first - j]);
  }
  for (int i = 1; i <= m; i++) printf("%lld\n", ans[i]);
}
int main() {
  prepare();
  solve();
}
