#include <bits/stdc++.h>
using namespace std;
const int N = 75005;
const int M = 200001;
const int MOD = 1e9;
int n, m, tot, root1[N], root2[N], lson[7500000], rson[7500000];
long long ans, val[7500000];
void ins(int p, int v, int l, int r, int& x, int y) {
  x = ++tot;
  val[x] = val[y] + v;
  if (l == r) return;
  int mid = (l + r) >> 1;
  if (p <= mid) {
    ins(p, v, l, mid, lson[x], lson[y]);
    rson[x] = rson[y];
  } else {
    lson[x] = lson[y];
    ins(p, v, mid + 1, r, rson[x], rson[y]);
  }
}
long long ask(int L, int R, int l, int r, int x) {
  if (!x) return 0LL;
  if (L <= l && R >= r) return val[x];
  int mid = (l + r) >> 1;
  long long t = 0;
  if (L <= mid) t += ask(L, R, l, mid, lson[x]);
  if (R > mid) t += ask(L, R, mid + 1, r, rson[x]);
  return t;
}
int main() {
  int i, j, l, r, x1, x2, a, b, y1, y2, x, y;
  scanf("%d", &n);
  for (i = 1; i <= n; ++i) {
    scanf("%d%d%d%d%d%d", &x1, &x2, &y1, &a, &b, &y2);
    ins(0, y1, 0, M, root2[i], root2[i - 1]);
    ins(x1 + 1, b - y1, 0, M, root2[i], root2[i]);
    ins(x2 + 1, y2 - b, 0, M, root2[i], root2[i]);
    ins(x1 + 1, a, 0, M, root1[i], root1[i - 1]);
    ins(x2 + 1, -a, 0, M, root1[i], root1[i]);
  }
  scanf("%d", &m);
  while (m--) {
    scanf("%d%d%d", &l, &r, &x);
    x = ((long long)x + ans) % MOD;
    y = x;
    if (x > M) x = M;
    long long A = ask(0, x, 0, M, root1[r]) - ask(0, x, 0, M, root1[l - 1]);
    long long B = ask(0, x, 0, M, root2[r]) - ask(0, x, 0, M, root2[l - 1]);
    ans = A * y + B;
    printf("%I64d\n", ans);
  }
  return 0;
}

### Prompt

Please formulate 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;
const int N = 75005;
const int M = 200001;
const int MOD = 1e9;
int n, m, tot, root1[N], root2[N], lson[7500000], rson[7500000];
long long ans, val[7500000];
void ins(int p, int v, int l, int r, int& x, int y) {
  x = ++tot;
  val[x] = val[y] + v;
  if (l == r) return;
  int mid = (l + r) >> 1;
  if (p <= mid) {
    ins(p, v, l, mid, lson[x], lson[y]);
    rson[x] = rson[y];
  } else {
    lson[x] = lson[y];
    ins(p, v, mid + 1, r, rson[x], rson[y]);
  }
}
long long ask(int L, int R, int l, int r, int x) {
  if (!x) return 0LL;
  if (L <= l && R >= r) return val[x];
  int mid = (l + r) >> 1;
  long long t = 0;
  if (L <= mid) t += ask(L, R, l, mid, lson[x]);
  if (R > mid) t += ask(L, R, mid + 1, r, rson[x]);
  return t;
}
int main() {
  int i, j, l, r, x1, x2, a, b, y1, y2, x, y;
  scanf("%d", &n);
  for (i = 1; i <= n; ++i) {
    scanf("%d%d%d%d%d%d", &x1, &x2, &y1, &a, &b, &y2);
    ins(0, y1, 0, M, root2[i], root2[i - 1]);
    ins(x1 + 1, b - y1, 0, M, root2[i], root2[i]);
    ins(x2 + 1, y2 - b, 0, M, root2[i], root2[i]);
    ins(x1 + 1, a, 0, M, root1[i], root1[i - 1]);
    ins(x2 + 1, -a, 0, M, root1[i], root1[i]);
  }
  scanf("%d", &m);
  while (m--) {
    scanf("%d%d%d", &l, &r, &x);
    x = ((long long)x + ans) % MOD;
    y = x;
    if (x > M) x = M;
    long long A = ask(0, x, 0, M, root1[r]) - ask(0, x, 0, M, root1[l - 1]);
    long long B = ask(0, x, 0, M, root2[r]) - ask(0, x, 0, M, root2[l - 1]);
    ans = A * y + B;
    printf("%I64d\n", ans);
  }
  return 0;
}

```