#include<iostream>
#include<algorithm>
#include<cstdio>
#define MAXN 100010
using namespace std;
int n,m,K,size=0,num=0;
int lsh[MAXN],root[MAXN];
struct Chairman_Tree{
int l,r,s;
long long sum;
}a[MAXN*40];
struct Question{
int x,v,c;
friend bool operator <(const Question &p,const Question &q){
return p.x<q.x;
}
}que[MAXN<<1];
inline int read(){
int date=0,w=1;char c=0;
while(c<‘0‘||c>‘9‘){if(c==‘-‘)w=-1;c=getchar();}
while(c>=‘0‘&&c<=‘9‘){date=date*10+c-‘0‘;c=getchar();}
return date*w;
}
inline void add_que(int x,int y,int v){
num++;
que[num].x=x;que[num].v=v;que[num].c=1;
num++;
que[num].x=y+1;que[num].v=v;que[num].c=-1;
}
inline void buildtree(){
root[0]=a[0].l=a[0].r=a[0].s=a[0].sum=0;
}
void insert(int k,int v,int l,int r,int &rt){
a[++size]=a[rt];rt=size;
a[rt].s+=v;a[rt].sum+=lsh[k]*v;
if(l==r)return;
int mid=l+r>>1;
if(k<=mid)insert(k,v,l,mid,a[rt].l);
else insert(k,v,mid+1,r,a[rt].r);
}
long long query(int k,int l,int r,int rt){
if(l==r)return (a[rt].sum/a[rt].s*k);
int mid=l+r>>1,t=a[a[rt].l].s;
if(k<=t)return query(k,l,mid,a[rt].l);
else return a[a[rt].l].sum+query(k-t,mid+1,r,a[rt].r);
}
void work(){
int x,k;
long long last=1,A,B,C;
while(m--){
x=read();A=read();B=read();C=read();k=(A*last+B)%C+1;
if(k>=a[root[x]].s)last=a[root[x]].sum;
else last=query(k,1,K,root[x]);
printf("%lld\n",last);
}
}
void init(){
int s,t,p;
n=read();m=read();
for(int i=1;i<=n;i++){
s=read();t=read();p=read();
add_que(s,t,p);
lsh[i]=p;
}
sort(lsh+1,lsh+n+1);
K=unique(lsh+1,lsh+n+1)-lsh-1;
sort(que+1,que+num+1);
buildtree();
for(int i=1,now=1;i<=m;i++){
root[i]=root[i-1];
while(now<=num&&que[now].x==i){
int x=lower_bound(lsh+1,lsh+K+1,que[now].v)-lsh;
insert(x,que[now].c,1,K,root[i]);
now++;
}
}
}
int main(){
init();
work();
return 0;
}