#include <bits/stdc++.h>
using namespace std;
int n, p, q, top, dp[4010], sta[20][4010], ans[200010];
vector<pair<int, int> > tr[200010 << 2][2];
void modify(int p, int L, int R, int l, int r, int op, int x, int y) {
  if (l <= L && R <= r) {
    tr[p][op].push_back(make_pair(x, y));
    return;
  }
  int mid = (L + R) >> 1;
  if (l <= mid) modify(p << 1, L, mid, l, r, op, x, y);
  if (r > mid) modify(p << 1 | 1, mid + 1, R, l, r, op, x, y);
}
void solve(int p, int l, int r) {
  ++top;
  for (int i = 1; i <= 4000; i++) sta[top][i] = dp[i];
  for (int i = 0; i < tr[p][0].size(); i++) {
    int c = tr[p][0][i].first, h = tr[p][0][i].second;
    for (int j = 4000; j >= c; j--) dp[j] = max(dp[j], dp[j - c] + h);
  }
  if (l == r) {
    for (int i = 0; i < tr[p][1].size(); i++)
      ans[tr[p][1][i].first] = dp[tr[p][1][i].second];
  } else {
    int mid = (l + r) >> 1;
    solve(p << 1, l, mid);
    solve(p << 1 | 1, mid + 1, r);
  }
  for (int i = 1; i <= 4000; i++) dp[i] = sta[top][i];
  --top;
}
int main() {
  ios::sync_with_stdio(0), cin.tie(0), cout.tie(0);
  cin >> n >> p;
  for (int i = 1; i <= n; i++) {
    int c, h, t;
    cin >> c >> h >> t;
    modify(1, 1, 20000, t, t + p - 1, 0, c, h);
  }
  cin >> q;
  for (int i = 1; i <= q; i++) {
    int a, b;
    cin >> a >> b;
    modify(1, 1, 20000, a, a, 1, i, b);
  }
  solve(1, 1, 20000);
  for (int i = 1; i <= q; i++) cout << ans[i] << "\n";
  return 0;
}
