#include <iostream>
#include <vector>
#include <algorithm>
using namespace std;
struct Melon {
int weight;
int maturity;
int gold;
};
int main() {
int n, q;
cin >> n >> q;
vector<Melon> melons(n);
for (int i = 0; i < n; i++) {
cin >> melons[i].weight >> melons[i].maturity >> melons[i].gold;
}
// Initialize a dynamic programming table
vector<vector<long long>> dp(501, vector<long long>(501, -1));
dp[0][0] = 0;
// Fill in the DP table
for (int i = 0; i < n; i++) {
for (int j = 500; j >= 0; j--) {
for (int k = 500; k >= 0; k--) {
if (dp[j][k] >= 0 && j + melons[i].weight <= 500) {
dp[j + melons[i].weight][min(k + melons[i].maturity, 500)] =
max(dp[j][k] + melons[i].gold, dp[j + melons[i].weight][min(k + melons[i].maturity, 500)]);
}
}
}
}
while (q--) {
int W, V;
cin >> W >> V;
long long result = -1;
// Find the maximum gold for the given constraints
for (int j = 0; j <= W; j++) {
for (int k = V; k <= 500; k++) {
result = max(result, dp[j][k]);
}
}
cout << result << endl;
}
return 0;
}