#include<bits/stdc++.h>
#define int long long
using namespace std;
inline int read()
{
int res=0,f=1;char c=getchar();
while(c<'0'||c>'9'){if(c=='-')f=-1;c=getchar();}
while(c>='0'&&c<='9'){res=(res<<1)+(res<<3)+(c^48);c=getchar();}
return res*f;
}
void write(int x)
{
if(x<0){x=-x;putchar('-');}
if(x>9)write(x/10);
putchar(x%10+'0');
}
int rt[114514];
struct segt
{
int ls,rs,val,cnt;
}t[10000005<<2];
int cnt;
#define mid (l+r>>1)
void update(int root)
{
t[root].val=t[t[root].ls].val+t[t[root].rs].val;
t[root].cnt=t[t[root].ls].cnt+t[t[root].rs].cnt;
}
int insert(int x,int l,int r,int root,int k)
{
if(!root)
root=++cnt;
if(l==r)
{
t[root].val+=x*k;
t[root].cnt+=k;
return root;
}
if(x<=mid)
t[root].ls=insert(x,l,mid,t[root].ls,k);
else
t[root].rs=insert(x,mid+1,r,t[root].rs,k);
update(root);
return root;
}
int merge(int x,int y,int l,int r)
{
if(!x||!y)
return x|y;
int cur=++cnt;
if(l==r)
{
t[cur].val=t[x].val+t[y].val;
t[cur].cnt=t[x].cnt+t[y].cnt;
return cur;
}
t[cur].ls=merge(t[x].ls,t[y].ls,l,mid);
t[cur].rs=merge(t[x].rs,t[y].rs,mid+1,r);
update(cur);
return cur;
}
int query_val(int x,int y,int l,int r,int root)
{
if(!root)
return 0;
if(x<=l&&r<=y)
return t[root].val;
int res=0;
if(x<=mid)
res+=query_val(x,y,l,mid,t[root].ls);
if(y>mid)
res+=query_val(x,y,mid+1,r,t[root].rs);
return res;
}
int query_cnt(int x,int y,int l,int r,int root)
{
if(!root)
return 0;
if(x<=l&&r<=y)
return t[root].cnt;
int res=0;
if(x<=mid)
res+=query_cnt(x,y,l,mid,t[root].ls);
if(y>mid)
res+=query_cnt(x,y,mid+1,r,t[root].rs);
return res;
}
int n,m;
int ans=1;
signed main()
{
// freopen("P3168_1.in","r",stdin);
// freopen("1.out","w",stdout);
n=read(),m=read();
for(int i=1;i<=n;i++)
{
int l=read(),r=read(),x=read();
rt[l]=insert(x,1,1e7,rt[l],1);
rt[r+1]=insert(x,1,1e7,rt[r+1],-1);
}
for(int i=2;i<=n;i++)
rt[i]=merge(rt[i],rt[i-1],1,1e7);
for(int i=1;i<=m;i++)
{
int x=read(),a=read(),b=read(),c=read();
int k=1+(a*ans+b)%c;
int l=1,r=1e7,pos;
while(l<=r)
{
if(query_cnt(1,mid,1,1e7,rt[x])<=k)
pos=mid,l=mid+1;
else
r=mid-1;
}
ans=query_val(1,pos,1,1e7,rt[x]);
write(ans),puts("");
}
}