#include <bits/stdc++.h>
using namespace std;
void boost() {
  ios_base::sync_with_stdio(0);
  cin.tie(0);
  cout.tie(0);
}
const int N = 1e6;
const long long inf = 1e9 + 7;
const long double PI = acos(-1);
long long bin_pow(long long x, long long y) {
  long long res = 1, base = x;
  while (y) {
    if (y & 1) {
      res *= base;
    }
    base *= base;
    y /= 2;
  }
  return res;
}
int n, p, m;
int dp1[4005][4005], dp2[4005][4005];
stack<pair<int, int> > st1, st2;
vector<int> goods[20005], questions[20005];
pair<int, int> a[10005];
int b[20005], ans[20005];
int curans1[4005], curans2[4005];
int main() {
  boost();
  cin >> n >> p;
  int mxt = -inf;
  for (int i = 0; i <= 4000; i++) {
    for (int j = 0; j <= 4000; j++) {
      dp1[i][j] = dp2[i][j] = -inf;
    }
    dp1[i][0] = dp2[i][0] = 0;
  }
  for (int i = 1; i <= n; i++) {
    int x;
    cin >> a[i].first >> a[i].second >> x;
    goods[x].push_back(i);
  }
  cin >> m;
  for (int i = 1; i <= m; i++) {
    int x;
    cin >> x >> b[i];
    mxt = max(mxt, x);
    questions[x].push_back(i);
  }
  for (int i = 1; i <= p; i++) {
    for (int j = 0; j < goods[i].size(); j++) {
      int x = goods[i][j];
      st1.push(make_pair(i, x));
      for (int k = 4000; k >= 0; k--) {
        dp1[st1.size()][k] = dp1[st1.size() - 1][k];
      }
      for (int k = 4000; k >= 0; k--) {
        if (k >= a[x].first && dp1[st1.size() - 1][k - a[x].first] != -inf) {
          dp1[st1.size()][k] =
              max(dp1[st1.size()][k],
                  dp1[st1.size()][k - a[x].first] + a[x].second);
        }
      }
    }
    if (questions[i].size()) {
      int mx = 0;
      for (int j = 0; j < questions[i].size(); j++) {
        mx = -inf;
        int x = questions[i][j];
        for (int k = b[x]; k >= 0; k--) {
          mx = max(mx, dp1[st1.size()][k]);
        }
        ans[x] = mx;
      }
    }
  }
  for (int i = p + 1; i <= mxt; i++) {
    if (st2.empty() && !st1.empty()) {
      while (!st1.empty()) {
        pair<int, int> x = st1.top();
        st1.pop();
        st2.push(x);
        for (int k = 4000; k >= 0; k--) {
          dp2[st2.size()][k] = dp2[st2.size() - 1][k];
        }
        for (int k = 4000; k >= 0; k--) {
          if (k >= a[x.second].first &&
              dp2[st2.size() - 1][k - a[x.second].first] != -inf) {
            dp2[st2.size()][k] =
                max(dp2[st2.size()][k], dp2[st2.size()][k - a[x.second].first] +
                                            a[x.second].second);
          }
        }
      }
    }
    while (!st2.empty() && (st2.top()).first <= i - p) {
      st2.pop();
    }
    if (goods[i].size()) {
      for (int j = 0; j < goods[i].size(); j++) {
        int x = goods[i][j];
        st1.push(make_pair(i, x));
        for (int k = 4000; k >= 0; k--) {
          dp1[st1.size()][k] = dp1[st1.size() - 1][k];
        }
        for (int k = 4000; k >= 0; k--) {
          if (k >= a[x].first && dp1[st1.size() - 1][k - a[x].first] != -inf) {
            dp1[st1.size()][k] =
                max(dp1[st1.size()][k],
                    dp1[st1.size()][k - a[x].first] + a[x].second);
          }
        }
      }
    }
    if (!questions[i].empty()) {
      for (int j = 1; j <= 4000; j++) {
        curans1[j] = max(curans1[j - 1], dp1[st1.size()][j]);
        curans2[j] = max(curans2[j - 1], dp2[st2.size()][j]);
      }
      int mx = -inf;
      for (int j = 0; j < questions[i].size(); j++) {
        int x = questions[i][j];
        int mx = -inf;
        for (int k = b[x]; k >= 0; k--) {
          mx = max(mx, curans1[k] + curans2[b[x] - k]);
          mx = max(mx, curans1[b[x] - k] + curans2[k]);
        }
        ans[x] = mx;
      }
    }
  }
  for (int i = 1; i <= m; i++) {
    cout << ans[i] << endl;
  }
}
