1 #include <iostream>
2 #include <cstdio>
3 #include <cstring>
4 #include <cstdlib>
5 #include <algorithm>
6 #define N (100000+5)
7 #define ll long long
8 using namespace std;
9 inline ll read()
10 {
11 char ch=getchar();ll kin=1,gi=0;
12 while(ch>‘9‘||ch<‘0‘){if(ch==‘-‘)kin=-1;ch=getchar();}
13 while(ch>=‘0‘&&ch<=‘9‘){gi=gi*10+ch-48;ch=getchar();}
14 return kin*gi;
15 }
16 struct tree
17 {
18 ll num;
19 ll simple;
20 ll son[2];
21 ll l,r,fl;
22 }d[5000005];
23 struct naive
24 {
25 ll nic;
26 ll st;
27 bool fl;
28 }ni[N*2];
29 bool cmp(naive x,naive y)
30 {
31 return x.st<y.st;
32 }
33 ll n,m,siz,rt[N];
34 ll sot[N],trl[N],kiz,fis,trik;
35 ll pre=1,lis[N],ra,rig;
36 void make(ll,ll,bool);
37 ll query(ll,ll);
38 void build(ll,ll,ll);
39 void rut(ll);
40 ll mmid(ll x)
41 {
42 ll l=1,r=kiz,res=0;
43 while(l<=r)
44 {
45 ll mid=(l+r)>>1;
46 if(trl[mid]<=x){l=mid+1;res=mid;}
47 else r=mid-1;
48 }
49 return res;
50 }
51 int main()
52 {
53 m=read();n=read();
54 for(ll i=1;i<=m;++i)
55 {
56
57 ni[++trik].st=read();ni[trik].fl=1;
58 ni[++trik].st=read()+1;ni[trik].fl=0;
59 ni[trik-1].nic=ni[trik].nic=read();
60 sot[i]=ni[trik].nic;
61 }
62 sort(sot+1,sot+m+1);
63 sort(ni+1,ni+trik+1,cmp);
64 for(ll i=1;i<=m;++i)if(sot[i]!=sot[i-1])trl[++kiz]=sot[i];
65 for(ll i=1;i<=trik;++i)
66 {
67 if(ni[i].st>n)break;
68 while(fis<ni[i].st){rut(++fis);}
69 ll rik=mmid(ni[i].nic);
70 make(ni[i].st,rik,ni[i].fl);
71 }
72 while(fis<n){rut(++fis);}
73 for(ll i=1;i<=n;++i)
74 {
75 ll sec=read();
76 ll a,b,c,k;
77 a=read();b=read();c=read();
78 k=(a*pre+b)%c+1;
79 printf("%lld\n",pre=query(sec,k));
80 }
81 return 0;
82 }
83 ll query(ll x,ll y)
84 {
85 ll nw=rt[x],res=0;
86 y=min(y,d[nw].simple);
87 while(1)
88 {
89 if(y==0)break;
90 if(d[nw].l==d[nw].r&&d[nw].simple>y){res+=trl[d[nw].l]*y;break;}
91 if(d[nw].simple<=y){res+=d[nw].num;break;}
92 if(d[d[nw].son[0]].simple<=y) y-=d[d[nw].son[0]].simple,res+=d[d[nw].son[0]].num,nw=d[nw].son[1];
93 else nw=d[nw].son[0];
94 }
95 return res;
96 }
97 void make(ll x,ll num,bool p)
98 {
99 ll nw=rt[x];bool f;
100 while(d[nw].l!=d[nw].r)
101 {
102 if(p)
103 {d[nw].num+=trl[num];d[nw].simple++;}
104 else
105 {d[nw].num-=trl[num];d[nw].simple--;}
106 f=(num>(d[nw].l+d[nw].r)>>1);
107 if(d[d[nw].son[f]].fl!=x)
108 {
109 d[++siz]=d[d[nw].son[f]];
110 d[nw].son[f]=siz;
111 d[siz].fl=x;
112 }
113 nw=d[nw].son[f];
114 }
115 if(p)
116 {d[nw].num+=trl[num];d[nw].simple++;}
117 else
118 {d[nw].num-=trl[num];d[nw].simple--;}
119 }
120 void rut(ll p)
121 {
122 ll x;
123 x=rt[p]=++siz;
124 d[x].l=1;d[x].r=kiz;d[x].fl=1;
125 if(x>1)
126 {
127 d[rt[p]]=d[rt[p-1]];
128 d[rt[p]].son[0]=d[rt[p-1]].son[0];
129 d[rt[p]].son[1]=d[rt[p-1]].son[1];
130 }
131 else
132 {
133 d[x].son[0]=++siz;
134 build(d[x].son[0],1,(kiz+1)>>1);
135 d[x].son[1]=++siz;
136 build(d[x].son[1],((kiz+1)>>1)+1,kiz);
137 }
138 }
139 void build(ll x,ll l,ll r)
140 {
141 d[x].l=l;d[x].r=r;d[x].fl=1;
142 if(l!=r)
143 {
144 d[x].son[0]=++siz;
145 build(siz,l,(l+r)>>1);
146 d[x].son[1]=++siz;
147 build(siz,((l+r)>>1)+1,r);
148 }
149 }