#include <bits/stdc++.h>
using namespace std;
bool debug = 0;
int n, m, k;
int dx[4] = {0, 1, 0, -1}, dy[4] = {1, 0, -1, 0};
string direc = "RDLU";
long long ln, lk, lm;
void etp(bool f = 0) {
  puts(f ? "YES" : "NO");
  exit(0);
}
void addmod(int &x, int y, int mod = 1000000007) {
  assert(y >= 0);
  x += y;
  if (x >= mod) x -= mod;
  assert(x >= 0 && x < mod);
}
void et() {
  puts("-1");
  exit(0);
}
struct mb {
  int x1, x2, a, b, y1, y2;
  void rd() { scanf("%d%d%d%d%d%d", &x1, &x2, &y1, &a, &b, &y2); }
} ynk[75005];
vector<int> vp;
int rs[4500000], cnt, lc[4500000], rc[4500000];
pair<long long, long long> T[4500000];
pair<long long, long long> add(pair<long long, long long> a,
                               pair<long long, long long> b) {
  return {a.first + b.first, a.second + b.second};
}
void upt(int rt, int &cur, int l, int r, int v,
         pair<long long, long long> val) {
  cur = ++cnt;
  if (l + 1 == r) {
    T[cur] = val;
    return;
  }
  int mid = (l + r) >> 1;
  if (v <= mid) {
    upt(lc[rt], lc[cur], l, mid, v, val);
    rc[cur] = rc[rt];
  } else {
    upt(rc[rt], rc[cur], mid, r, v, val);
    lc[cur] = lc[rt];
  }
  T[cur] = add(T[lc[cur]], T[rc[cur]]);
}
pair<long long, long long> qy(int rt, int l, int r, int L, int R) {
  if (R <= l || r <= L) return {0, 0};
  if (L <= l && r <= R) {
    return T[rt];
  }
  int mid = (l + r) >> 1;
  pair<long long, long long> x = qy(lc[rt], l, mid, L, R);
  pair<long long, long long> y = qy(rc[rt], mid, r, L, R);
  return add(x, y);
}
void build(int &rt, int l, int r) {
  if (rt == 0) rt = ++cnt;
  if (l + 1 == r) {
    T[rt] = {0, ynk[r].y1};
    return;
  }
  int mid = (l + r) / 2;
  build(lc[rt], l, mid);
  build(rc[rt], mid, r);
  T[rt] = add(T[lc[rt]], T[rc[rt]]);
}
vector<pair<int, int> > eve[200005];
int ME = 200002;
void fmain(int tid) {
  scanf("%d", &n);
  for (int(i) = 1; (i) <= (int)(n); (i)++) ynk[i].rd();
  vp.push_back(-(1 << 30));
  vector<pair<int, int> > tmp;
  for (int(i) = 1; (i) <= (int)(n); (i)++) {
    eve[ynk[i].x1 + 1].push_back({i, 1});
    eve[ynk[i].x2 + 1].push_back({i, 2});
  }
  build(rs[0], 0, n);
  for (int(i) = 1; (i) <= (int)(ME); (i)++)
    if (!eve[i].empty()) {
      vp.push_back(i);
      int j = vp.size() - 1;
      int pre = rs[j - 1];
      for (auto p : eve[i]) {
        int id = p.first;
        if (p.second == 1) {
          upt(pre, rs[j], 0, n, id, {ynk[id].a, ynk[id].b});
        } else {
          upt(pre, rs[j], 0, n, id, {0, ynk[id].y2});
        }
        pre = rs[j];
      }
    }
  scanf("%d", &m);
  long long ans = 0;
  for (int(i) = 1; (i) <= (int)(m); (i)++) {
    int l, r, x;
    scanf("%d%d%d", &l, &r, &x);
    x = (ans + x) % (1000000000);
    auto it = upper_bound(vp.begin(), vp.end(), x) - vp.begin();
    it--;
    pair<long long, long long> vec = qy(rs[it], 0, n, l - 1, r);
    ans = vec.first * x + vec.second;
    printf("%lld\n", ans);
  }
}
int main() {
  int t = 1;
  for (int(i) = 1; (i) <= (int)(t); (i)++) {
    fmain(i);
  }
  return 0;
}

### Prompt

Your challenge is to write a CPP solution to the following problem:

You have an array f of n functions.The function fi(x) (1 ≤ i ≤ n) is characterized by parameters: x1, x2, y1, a, b, y2 and take values: 

  * y1, if x ≤ x1. 
  * a·x + b, if x1 < x ≤ x2. 
  * y2, if x > x2. 



There are m queries. Each query is determined by numbers l, r and x. For a query with number i (1 ≤ i ≤ m), you need to calculate the sum of all fj(xi) where l ≤ j ≤ r. The value of xi is calculated as follows: xi = (x + last) mod 109, where last is the answer to the query with number i - 1. The value of last equals 0 if i = 1.

Input

First line contains one integer number n (1 ≤ n ≤ 75000).

Each of the next n lines contains six integer numbers: x1, x2, y1, a, b, y2 (0 ≤ x1 < x2 ≤ 2·105, 0 ≤ y1, y2 ≤ 109, 0 ≤ a, b ≤ 104).

Next line contains one integer number m (1 ≤ m ≤ 500000).

Each of the next m lines contains three integer numbers: l, r and x (1 ≤ l ≤ r ≤ n, 0 ≤ x ≤ 109).

Examples

Input

1
1 2 1 4 5 10
1
1 1 2


Output

13


Input

3
2 5 1 1 1 4
3 6 8 2 5 7
1 3 5 1 4 10
3
1 3 3
2 3 2
1 2 5


Output

19
17
11

### Response

```cpp
#include <bits/stdc++.h>
using namespace std;
bool debug = 0;
int n, m, k;
int dx[4] = {0, 1, 0, -1}, dy[4] = {1, 0, -1, 0};
string direc = "RDLU";
long long ln, lk, lm;
void etp(bool f = 0) {
  puts(f ? "YES" : "NO");
  exit(0);
}
void addmod(int &x, int y, int mod = 1000000007) {
  assert(y >= 0);
  x += y;
  if (x >= mod) x -= mod;
  assert(x >= 0 && x < mod);
}
void et() {
  puts("-1");
  exit(0);
}
struct mb {
  int x1, x2, a, b, y1, y2;
  void rd() { scanf("%d%d%d%d%d%d", &x1, &x2, &y1, &a, &b, &y2); }
} ynk[75005];
vector<int> vp;
int rs[4500000], cnt, lc[4500000], rc[4500000];
pair<long long, long long> T[4500000];
pair<long long, long long> add(pair<long long, long long> a,
                               pair<long long, long long> b) {
  return {a.first + b.first, a.second + b.second};
}
void upt(int rt, int &cur, int l, int r, int v,
         pair<long long, long long> val) {
  cur = ++cnt;
  if (l + 1 == r) {
    T[cur] = val;
    return;
  }
  int mid = (l + r) >> 1;
  if (v <= mid) {
    upt(lc[rt], lc[cur], l, mid, v, val);
    rc[cur] = rc[rt];
  } else {
    upt(rc[rt], rc[cur], mid, r, v, val);
    lc[cur] = lc[rt];
  }
  T[cur] = add(T[lc[cur]], T[rc[cur]]);
}
pair<long long, long long> qy(int rt, int l, int r, int L, int R) {
  if (R <= l || r <= L) return {0, 0};
  if (L <= l && r <= R) {
    return T[rt];
  }
  int mid = (l + r) >> 1;
  pair<long long, long long> x = qy(lc[rt], l, mid, L, R);
  pair<long long, long long> y = qy(rc[rt], mid, r, L, R);
  return add(x, y);
}
void build(int &rt, int l, int r) {
  if (rt == 0) rt = ++cnt;
  if (l + 1 == r) {
    T[rt] = {0, ynk[r].y1};
    return;
  }
  int mid = (l + r) / 2;
  build(lc[rt], l, mid);
  build(rc[rt], mid, r);
  T[rt] = add(T[lc[rt]], T[rc[rt]]);
}
vector<pair<int, int> > eve[200005];
int ME = 200002;
void fmain(int tid) {
  scanf("%d", &n);
  for (int(i) = 1; (i) <= (int)(n); (i)++) ynk[i].rd();
  vp.push_back(-(1 << 30));
  vector<pair<int, int> > tmp;
  for (int(i) = 1; (i) <= (int)(n); (i)++) {
    eve[ynk[i].x1 + 1].push_back({i, 1});
    eve[ynk[i].x2 + 1].push_back({i, 2});
  }
  build(rs[0], 0, n);
  for (int(i) = 1; (i) <= (int)(ME); (i)++)
    if (!eve[i].empty()) {
      vp.push_back(i);
      int j = vp.size() - 1;
      int pre = rs[j - 1];
      for (auto p : eve[i]) {
        int id = p.first;
        if (p.second == 1) {
          upt(pre, rs[j], 0, n, id, {ynk[id].a, ynk[id].b});
        } else {
          upt(pre, rs[j], 0, n, id, {0, ynk[id].y2});
        }
        pre = rs[j];
      }
    }
  scanf("%d", &m);
  long long ans = 0;
  for (int(i) = 1; (i) <= (int)(m); (i)++) {
    int l, r, x;
    scanf("%d%d%d", &l, &r, &x);
    x = (ans + x) % (1000000000);
    auto it = upper_bound(vp.begin(), vp.end(), x) - vp.begin();
    it--;
    pair<long long, long long> vec = qy(rs[it], 0, n, l - 1, r);
    ans = vec.first * x + vec.second;
    printf("%lld\n", ans);
  }
}
int main() {
  int t = 1;
  for (int(i) = 1; (i) <= (int)(t); (i)++) {
    fmain(i);
  }
  return 0;
}

```