#include <bits/stdc++.h>
using namespace std;
struct Product {
  int price, happiness, time;
  Product(int price, int happiness, int time)
      : price(price), happiness(happiness), time(time){};
  bool operator<(const Product &other) const { return time < other.time; }
};
struct Visit {
  int budget, time, index;
  int result;
  Visit(int budget, int time, int index)
      : budget(budget), time(time), index(index), result(0){};
  bool operator<(const Visit &other) const { return time < other.time; }
};
int dp[4001][4001];
int main() {
  ios_base::sync_with_stdio(false);
  int n, p, q;
  cin >> n >> p;
  vector<Product> products;
  for (int i = 0; i < n; i++) {
    int price, happiness, time;
    cin >> price >> happiness >> time;
    products.emplace_back(price, happiness, time);
  }
  sort(products.begin(), products.end());
  cin >> q;
  vector<Visit> visits;
  for (int i = 0; i < q; i++) {
    int time, budget;
    cin >> time >> budget;
    visits.emplace_back(budget, time, i);
  }
  sort(visits.begin(), visits.end());
  int center_product_index = 0;
  int query_index = 0;
  int leftmost_product_index = 0;
  int rightmost_product_index = 0;
  for (int center_time = 0; center_time <= 20000; center_time += p) {
    for (; center_product_index < n &&
           products[center_product_index].time <= center_time;
         center_product_index++)
      ;
    for (int budget = 0; budget <= 4000; budget++) {
      dp[center_product_index][budget] = 0;
    }
    for (int i = center_product_index - 1;
         i >= 0 && products[i].time >= center_time - (p - 1); i--) {
      dp[i][0] = 0;
      leftmost_product_index = i;
      for (int budget = 1; budget <= 4000; budget++) {
        if (budget >= products[i].price)
          dp[i][budget] =
              max(dp[i + 1][budget], dp[i + 1][budget - products[i].price] +
                                         products[i].happiness);
        else
          dp[i][budget] = dp[i + 1][budget];
      }
    }
    for (int i = center_product_index;
         i < n && products[i].time <= center_time + (p - 1); i++) {
      dp[i + 1][0] = 0;
      for (int budget = 1; budget <= 4000; budget++) {
        if (budget >= products[i].price)
          dp[i + 1][budget] =
              max(dp[i][budget],
                  dp[i][budget - products[i].price] + products[i].happiness);
        else
          dp[i + 1][budget] = dp[i][budget];
      }
    }
    rightmost_product_index = center_product_index - 1;
    for (; query_index < q && visits[query_index].time <= center_time + (p - 1);
         query_index++) {
      for (; leftmost_product_index < n &&
             products[leftmost_product_index].time <=
                 visits[query_index].time - p;
           leftmost_product_index++)
        ;
      for (; rightmost_product_index + 1 < n &&
             products[rightmost_product_index + 1].time <=
                 visits[query_index].time;
           rightmost_product_index++)
        ;
      for (int budget = 0; budget <= visits[query_index].budget; budget++) {
        visits[query_index].result =
            max(visits[query_index].result,
                dp[leftmost_product_index][budget] +
                    dp[rightmost_product_index + 1]
                      [visits[query_index].budget - budget]);
      }
    }
  }
  vector<int> res(q);
  for (int i = 0; i < q; i++) {
    res[visits[i].index] = visits[i].result;
  }
  for (int i = 0; i < q; i++) {
    cout << res[i] << endl;
  }
}
