#include <bits/stdc++.h>
using namespace std;
#define dbgv(v) cout<<#v<<" = "; f(i,0,v.size()) cout<<v[i]<<" "; cout<<endl
#define dbga(a,x,y) cout<<#a<<" = "; f(i,x,y) cout<<a[i]<<" "; cout<<endl
#define erorp(x) cout<<#x<<"={"<<x.F<<" , "<<x.S<<"}"<<endl
#define eror(x) cout<<#x<<'='<<(x)<<endl
#define f_(i,a,b) for(int i=a;i>=b;i--)
#define f(i,a,b) for(int i=a;i<b;i++)
#define nb(x) __builtin_popcount(x)
#define all(v) v.begin(),v.end()
#define bit(n,k) (((n)>>(k))&1)
#define Add(x,y) x=(x+y)%mod
#define maxm(a,b) a=max(a,b)
#define minm(a,b) a=min(a,b)
#define lst(x) x[x.size()-1]
#define sz(x) int(x.size())
#define mp make_pair
#define ll long long
#define pb push_back
#define S second
#define F first
const int N=1e4;
ll n,q,T[N],A[N],B[N],C[N],dp[N][N],nxt[N][N],prv[N][N];
int32_t main(){
ios:: sync_with_stdio(0), cin.tie(0), cout.tie(0);
cin>>n>>q;
f(i,0,n){
cin>>T[i]>>A[i]>>B[i]>>C[i];
T[i]<<=1;
A[i]<<=1;
B[i]<<=1;
C[i]>>=1;
if(A[i]<B[i]){
f(pos,A[i],B[i]){
nxt[T[i]+pos-A[i]][pos]+=C[i];
}
}
else{
f_(pos,A[i],B[i]+1){
prv[T[i]+A[i]-pos][pos]+=C[i];
}
}
}
f_(i,N-2,1){
f(j,1,N-2){
dp[i][j]=max(dp[i+1][j+1]+nxt[i][j],dp[i+1][j-1]+prv[i][j]);
}
}
while(q--){
int p,x;
cin>>p>>x;
cout<<dp[p<<1][x<<1]<<'\n';
}
}
/*
2 1
1 2 1 4
3 1 3 2
3 3
*/
Compilation message
/usr/bin/ld: failed to convert GOTPCREL relocation; relink with --no-relax
collect2: error: ld returned 1 exit status