#include<cstdio>
#include<cstdlib>
#include<algorithm>
#include<cstring>
#include<vector>
#define MAXN 100000+10
#define Nd Node*
#define pii pair<int,int>
#define mp make_pair
#define ft first
#define sc second
#define pb push_back
#define ll long long
using namespace std;
struct Node{
Nd l;Nd r;
int L,R,cnt;
ll sum;
};
Nd rt[MAXN];
ll b[MAXN];
Nd NewNode(Nd l,Nd r,int L,int R,ll sum,int cnt){
Nd ret=new Node;
ret->l=l,ret->r=r,ret->L=L,ret->R=R,ret->sum=sum,ret->cnt=cnt;
return ret;
}
Nd Build(int L,int R){
Nd ret=NewNode(NULL,NULL,L,R,0LL,);
if(L+!=R)ret->l=Build(L,(L+R)>>),ret->r=Build((L+R)>>,R);
return ret;
}
void update(Nd &A,Nd B,int p,ll x,int f){
if(!B)return;
A=NewNode(B->l,B->r,B->L,B->R,B->sum+f*x,B->cnt+f);
if(p<(B->L+B->R)>>)update(A->l,B->l,p,x,f);
else update(A->r,B->r,p,x,f);
}
ll query(Nd A,int k){
if(!A->l)return k*b[A->L];
if(A->l->cnt>=k)return query(A->l,k);
else return query(A->r,k-A->l->cnt)+A->l->sum;
}
struct Opt{
int L,R,p;
ll x;
}s[MAXN];
int n,m,sz;
vector<pii> vs[MAXN];
void init(){
scanf("%d%d",&m,&n);
for(int i=;i<=m;i++){
scanf("%d%d%lld",&s[i].L,&s[i].R,&s[i].x);
b[i]=s[i].x;
}
sort(b+,b+m+);
sz=unique(b+,b+m+)-(b+);
rt[]=Build(,sz+);
for(int i=;i<=m;i++){
s[i].p=lower_bound(b+,b+m+,s[i].x)-b;
vs[s[i].L].pb(mp(s[i].p,));
vs[s[i].R+].pb(mp(s[i].p,-));
}
for(int i=;i<=n;i++){
rt[i]=rt[i-];
for(int j=;j<vs[i].size();j++){
pii &t=vs[i][j];
update(rt[i],rt[i],t.ft,b[t.ft],t.sc);
}
}
}
void solve(){
ll pre=,X,A,B,C;
int k;
for(int i=;i<=n;i++){
scanf("%lld%lld%lld%lld",&X,&A,&B,&C);
k=(A*pre+B)%C+;
pre=(k>=rt[X]->cnt?rt[X]->sum:query(rt[X],k));
printf("%lld\n",pre);
}
}
int main()
{
//freopen("data.in","r",stdin);
init();
solve();
return ;
}