#include<bits/stdc++.h>
#define re register
#define in inline
#define int long long
using namespace std;
in int max(int x,int y){return x>y?x:y;}
in int min(int x,int y){return x<y?x:y;}
int n,q,ans;
struct node{
int c,be,fr;
}a[501];
int f[501][501],vis[501];
in void dfs(int x,int y,int z){
if(f[x][y]<z)f[x][y]=z;
for(int i=1;i<=n;i++){
if(vis[i])continue;
vis[i]=1;
dfs(x+a[i].c,y+a[i].fr,z+a[i].be);
vis[i]=0;
}
}
signed main(){
cin>>n>>q;
for(re int i=1;i<=n;i++){
cin>>a[i].c>>a[i].fr>>a[i].be;
}
dfs(0,0,0);
for(int i=1;i<=500;i++){
for(int j=500;j>=1;j--){
if(f[i][j]<f[i][j+1])f[i][j]=f[i][j+1];
}
}
while(q--){
int x,y;
cin>>x>>y;
cout<<f[x][y]<<endl;
}
}