#include <iostream>
#include <climits>
#include <random>
#include <vector>
#include <ctime>
using namespace std;
inline int read()
{
int ans = 0, f = 0;
char c = getchar();
while (!isdigit(c))
f |= (c == '-'), c = getchar();
while (isdigit(c))
ans = (ans << 3) + (ans << 1) + c - 48, c = getchar();
return f ? -ans : ans;
}
void write(int x)
{
if (x < 0)
putchar('-'), x = -x;
if (x > 9)
write(x / 10);
putchar(48 + x % 10);
}
const int N = 1e5 + 5;
int m, n, root[N];
vector<int> S[N], T[N];
int cnt, val[N], sum[N];
int si[N], pr[N], son[N][2];
inline void clone(int x, int y)
{
si[x] = si[y], pr[x] = pr[y];
val[x] = val[y], sum[x] = sum[y];
son[x][0] = son[y][0], son[x][1] = son[y][1];
}
inline int _new(int v)
{
pr[++cnt] = rand(), si[cnt] = 1;
val[cnt] = sum[cnt] = v;
return cnt;
}
inline void updata(int p)
{
si[p] = 1 + si[son[p][0]] + si[son[p][1]];
sum[p] = val[p] + sum[son[p][0]] + sum[son[p][1]];
}
void split(int p, int k, int &x, int &y)
{
if (!p)
{
x = y = 0;
return;
}
if (val[p] <= k)
{
x = ++cnt;
clone(x, p);
split(son[x][1], k, son[x][1], y);
updata(x);
}
else
{
y = ++cnt;
clone(y, p);
split(son[y][0], k, x, son[y][0]);
updata(y);
}
}
int merge(int x, int y)
{
if (!x || !y)
return x | y;
if (pr[x] > pr[y])
{
int p = ++cnt;
clone(p, x);
son[p][1] = merge(son[p][1], y);
updata(p);
return p;
}
else
{
int p = ++cnt;
clone(p, y);
son[p][0] = merge(x, son[p][0]);
updata(p);
return p;
}
}
inline int rev(int p, int k)
{
int ans = 0;
while (true)
{
cout << "p " << p << '\n';
if (si[son[p][0]] >= k)
p = son[p][0];
else
{
ans += sum[son[p][0]] + val[p];
cout << "p " << p << " VAL " << val[p] << '\n';
if (k == si[son[p][0]] + 1)
return ans;
k -= si[son[p][0]] + 1;
p = son[p][1];
}
}
}
signed main()
{
srand(time(0));
m = read(), n = read();
while (m--)
{
int s = read(), t = read(), p = read();
S[s].push_back(p);
T[t + 1].push_back(p);
}
for (int i = 1; i <= n; ++i)
{
root[i] = ++cnt;
clone(root[i], root[i - 1]);
for (int l = 0; l < S[i].size(); ++l)
{
int p = S[i][l];
int x, y;
split(root[i], p, x, y);
root[i] = merge(merge(x, _new(p)), y);
}
for (int l = 0; l < T[i].size(); ++l)
{
int p = T[i][l];
int x, y, x1, y1;
split(root[i], p - 1, x, y);
split(y, p, x1, y1);
root[i] = merge(x, merge(merge(son[x1][0], son[x1][1]), y1));
}
}
int pre = 1;
while (n--)
{
int x = read(), k = 1 + (read() * pre + read()) % read();
k = min(k, si[root[x]]);
write(rev(root[x], k));
putchar('\n');
}
return 0;
}