#include <bits/stdc++.h>
using namespace std;
template <class T>
inline T sqr(T x) {
  return x * x;
}
template <class T>
inline string tostr(const T& x) {
  stringstream ss;
  ss << x;
  return ss.str();
}
inline long long parse(const string& s) {
  stringstream ss(s);
  long long x;
  ss >> x;
  return x;
}
const double EPS = 1e-9;
const int inf = 1000 * 1000 * 1000;
const char cinf = 102;
const long long linf = inf * 1ll * inf;
const double dinf = 1e200;
const double PI = 3.1415926535897932384626433832795l;
int gcd(int a, int b) { return a ? gcd(b % a, a) : b; }
long long gcd(long long a, long long b) { return a ? gcd(b % a, a) : b; }
long long powmod(long long a, long long p, long long m) {
  long long r = 1;
  while (p) {
    if (p & 1) r = r * a % m;
    p >>= 1;
    a = a * a % m;
  }
  return r;
}
using namespace std;
int n, p, q, dp[4000][4001][2], pl[30001], pr[30001], res[20000];
vector<pair<int, int>> items[30001], queries[30001];
void prepare() {
  scanf("%d %d", &n, &p);
  for (int i = 0; i < n; i++) {
    int cost, addict, time;
    scanf("%d %d %d", &cost, &addict, &time);
    items[time + p].push_back(make_pair(cost, addict));
  }
  scanf("%d", &q);
  for (int i = 0; i < q; i++) {
    int time, money;
    scanf("%d %d", &time, &money);
    queries[time].push_back(make_pair(money, i));
  }
  for (int i = 0; i <= 30000; ++i) pr[i] = pl[i] = -1;
}
void upd(int cnt, int sum, int cost, int addict, int t) {
  if (cnt) dp[cnt][sum][t] = max(dp[cnt][sum][t], dp[cnt - 1][sum][t]);
  dp[cnt][sum][t] = max(dp[cnt][sum][t], dp[cnt][sum - 1][t]);
  if (sum >= cost && cnt)
    dp[cnt][sum][t] = max(dp[cnt][sum][t], dp[cnt - 1][sum - cost][t] + addict);
  else if (sum >= cost)
    dp[cnt][sum][t] = max(dp[cnt][sum][t], addict);
}
void clear(int cntl, int cntr, int l, int r) {
  for (int i = l; i <= r; i++) pl[i] = pr[i] = -1;
  for (int i = 0; i <= cntl; i++)
    for (int j = 1; j <= 4000; j++) dp[i][j][0] = 0;
  for (int i = 0; i <= cntr; i++)
    for (int j = 1; j <= 4000; j++) dp[i][j][1] = 0;
}
int main() {
  prepare();
  for (int l = 0; l * p <= 30000; ++l) {
    int center = min(30000, (l + 1) * p + 1);
    int cntl = -1;
    int cntr = -1;
    for (int i = center - 1; i >= l * p; --i) {
      for (int j = 0; j < items[i].size(); j++) {
        cntl++;
        for (int sum = 1; sum <= 4000; sum++) {
          upd(cntl, sum, items[i][j].first, items[i][j].second, 0);
        }
      }
      pl[i] = cntl;
    }
    for (int i = center; i <= min(30000, (l + 2) * p); i++) {
      for (int j = 0; j < items[i].size(); j++) {
        cntr++;
        for (int sum = 1; sum <= 4000; sum++) {
          upd(cntr, sum, items[i][j].first, items[i][j].second, 1);
        }
      }
      pr[i] = cntr;
    }
    for (int i = l * p + 1; i < center; i++) {
      for (int j = 0; j < queries[i].size(); j++) {
        int asdleft = pl[i + 1];
        int asdright = pr[min(30000, i + p)];
        int doll = queries[i][j].first;
        int id = queries[i][j].second;
        for (int sLeft = 0; sLeft <= doll; sLeft++) {
          int pa = 0;
          if (asdleft >= 0) pa += dp[asdleft][sLeft][0];
          if (asdright >= 0) pa += dp[asdright][doll - sLeft][1];
          res[id] = max(res[id], pa);
        }
      }
    }
    clear(cntl, cntr, l * p, min(30000, (l + 2) * p));
  }
  for (int i = 0; i < q; ++i) printf("%d\n", res[i]);
  return 0;
}
