#include <bits/stdc++.h>
using namespace std;
template <class X, class Y>
bool minimize(X &x, const Y &y) {
  X eps = 1e-9;
  if (x > y + eps) {
    x = y;
    return true;
  } else
    return false;
}
template <class X, class Y>
bool maximize(X &x, const Y &y) {
  X eps = 1e-9;
  if (x + eps < y) {
    x = y;
    return true;
  } else
    return false;
}
template <class T>
T Abs(const T &x) {
  return (x < 0 ? -x : x);
}
struct Item {
  int cost, happiness, time;
  Item() { cost = happiness = time = 0; }
  Item(int t) {
    cost = happiness = 0;
    time = t;
  }
  void input(void) { scanf("%d%d%d", &cost, &happiness, &time); }
  bool operator<(const Item &i) const { return time < i.time; }
};
struct Query {
  int ___left, ___right, budget, answer;
  Query(int ___left = 0, int ___right = 0, int budget = 0) {
    this->___left = ___left;
    this->___right = ___right;
    this->budget = budget;
    this->answer = 0;
  }
};
const int INF = (int)1e9 + 7;
Item items[4040];
Query queries[20020];
int numItem, numQuery, duration;
int maxHappyLeft[4040][4040 + 3], maxHappyRight[4040][4040 + 3];
void init(void) {
  scanf("%d%d", &numItem, &duration);
  for (int i = (1), _b = (numItem); i <= _b; i++) items[i].input();
  sort(items + 1, items + numItem + 1);
  scanf("%d", &numQuery);
  for (int i = (1), _b = (numQuery); i <= _b; i++) {
    int pos, budget;
    scanf("%d%d", &pos, &budget);
    int l = upper_bound(items + 1, items + numItem + 1, Item(pos - duration)) -
            items;
    int r = upper_bound(items + 1, items + numItem + 1, Item(pos)) - items;
    queries[i] = Query(l, r - 1, budget);
  }
}
void solve(int l, int r, const vector<int> &indices) {
  if (l > r) return;
  if (l == r) {
    int cost = items[r].cost;
    int happiness = items[r].happiness;
    for (__typeof((indices).begin()) it = (indices).begin();
         it != (indices).end(); it++) {
      Query &query = queries[*it];
      if (query.___left <= query.___right)
        query.answer = query.budget >= cost ? happiness : 0;
    }
    return;
  }
  int m = (l + r) >> 1;
  for (int i = (l), _b = (m); i <= _b; i++)
    for (int j = (0), _b = (4040); j <= _b; j++) maxHappyLeft[i][j] = -INF;
  for (int i = (m + 1), _b = (r); i <= _b; i++)
    for (int j = (0), _b = (4040); j <= _b; j++) maxHappyRight[i][j] = -INF;
  for (int j = (0), _b = (4040); j <= _b; j++)
    maxHappyLeft[m][j] = items[m].cost <= j ? items[m].happiness : 0;
  for (int i = (m - 1), _a = (l); i >= _a; i--)
    for (int j = (0), _b = (4040); j <= _b; j++) {
      maxHappyLeft[i][j] = maxHappyLeft[i + 1][j];
      if (j >= items[i].cost)
        maximize(maxHappyLeft[i][j],
                 maxHappyLeft[i + 1][j - items[i].cost] + items[i].happiness);
    }
  for (int j = (0), _b = (4040); j <= _b; j++)
    maxHappyRight[m + 1][j] =
        items[m + 1].cost <= j ? items[m + 1].happiness : 0;
  for (int i = (m + 2), _b = (r); i <= _b; i++)
    for (int j = (0), _b = (4040); j <= _b; j++) {
      maxHappyRight[i][j] = maxHappyRight[i - 1][j];
      if (j >= items[i].cost)
        maximize(maxHappyRight[i][j],
                 maxHappyRight[i - 1][j - items[i].cost] + items[i].happiness);
    }
  vector<int> leftIndices, rightIndices;
  for (__typeof((indices).begin()) it = (indices).begin();
       it != (indices).end(); it++) {
    Query &query = queries[*it];
    if (query.___right <= m)
      leftIndices.push_back(*it);
    else if (query.___left > m)
      rightIndices.push_back(*it);
    else if (query.___left <= m && query.___right > m) {
      query.answer = -INF;
      for (int j = (0), _b = (query.budget); j <= _b; j++)
        maximize(query.answer,
                 maxHappyLeft[query.___left][j] +
                     maxHappyRight[query.___right][query.budget - j]);
    }
  }
  solve(l, m, leftIndices);
  solve(m + 1, r, rightIndices);
}
void process(void) {
  vector<int> indices;
  for (int i = (1), _b = (numQuery); i <= _b; i++) indices.push_back(i);
  solve(1, numItem, indices);
  for (int i = (1), _b = (numQuery); i <= _b; i++)
    printf("%d\n", queries[i].answer);
}
int main(void) {
  init();
  process();
  return 0;
}
