#include <bits/stdc++.h>
using namespace std;
long long taga[10000100], tagb[10000100];
int lch[10000100], rch[10000100];
int tot, n, T, root[75010];
long long ans, A, B;
int modify(int pre, int l, int r, int L, int R, long long a, long long b) {
  int now = ++tot;
  if (L <= l && r <= R) {
    taga[now] = taga[pre] + a;
    tagb[now] = tagb[pre] + b;
    lch[now] = lch[pre], rch[now] = rch[pre];
  } else {
    int mid = (l + r) >> 1;
    if (L <= mid)
      lch[now] = modify(lch[pre], l, mid, L, R, a, b);
    else
      lch[now] = lch[pre];
    if (mid < R)
      rch[now] = modify(rch[pre], mid + 1, r, L, R, a, b);
    else
      rch[now] = rch[pre];
    taga[now] = taga[pre];
    tagb[now] = tagb[pre];
  }
  return now;
}
void query(int root, int l, int r, int x) {
  A += taga[root];
  B += tagb[root];
  if (l == r) return;
  int mid = (l + r) >> 1;
  if (x <= mid)
    query(lch[root], l, mid, x);
  else
    query(rch[root], mid + 1, r, x);
}
int main() {
  scanf("%d", &n);
  root[0] = lch[0] = rch[0] = 0;
  for (int i = 1; i <= n; i++) {
    int x1, x2, y1, y2, a, b;
    scanf("%d%d%d%d%d%d", &x1, &x2, &y1, &a, &b, &y2);
    root[i] = root[i - 1];
    root[i] = modify(root[i], 0, 200001, 1, x1, 0, y1);
    root[i] = modify(root[i], 0, 200001, x1 + 1, x2, a, b);
    root[i] = modify(root[i], 0, 200001, x2 + 1, 200001, 0, y2);
  }
  scanf("%d", &T);
  while (T--) {
    int l, r, x;
    scanf("%d%d%d", &l, &r, &x);
    x = (ans + x) % 1000000000;
    x = min(x, 200001);
    A = B = 0;
    query(root[r], 0, 200001, x);
    ans = A * x + B;
    A = B = 0;
    query(root[l - 1], 0, 200001, x);
    ans -= A * x + B;
    printf("%I64d\n", ans);
  }
  return 0;
}

### Prompt

Generate 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;
long long taga[10000100], tagb[10000100];
int lch[10000100], rch[10000100];
int tot, n, T, root[75010];
long long ans, A, B;
int modify(int pre, int l, int r, int L, int R, long long a, long long b) {
  int now = ++tot;
  if (L <= l && r <= R) {
    taga[now] = taga[pre] + a;
    tagb[now] = tagb[pre] + b;
    lch[now] = lch[pre], rch[now] = rch[pre];
  } else {
    int mid = (l + r) >> 1;
    if (L <= mid)
      lch[now] = modify(lch[pre], l, mid, L, R, a, b);
    else
      lch[now] = lch[pre];
    if (mid < R)
      rch[now] = modify(rch[pre], mid + 1, r, L, R, a, b);
    else
      rch[now] = rch[pre];
    taga[now] = taga[pre];
    tagb[now] = tagb[pre];
  }
  return now;
}
void query(int root, int l, int r, int x) {
  A += taga[root];
  B += tagb[root];
  if (l == r) return;
  int mid = (l + r) >> 1;
  if (x <= mid)
    query(lch[root], l, mid, x);
  else
    query(rch[root], mid + 1, r, x);
}
int main() {
  scanf("%d", &n);
  root[0] = lch[0] = rch[0] = 0;
  for (int i = 1; i <= n; i++) {
    int x1, x2, y1, y2, a, b;
    scanf("%d%d%d%d%d%d", &x1, &x2, &y1, &a, &b, &y2);
    root[i] = root[i - 1];
    root[i] = modify(root[i], 0, 200001, 1, x1, 0, y1);
    root[i] = modify(root[i], 0, 200001, x1 + 1, x2, a, b);
    root[i] = modify(root[i], 0, 200001, x2 + 1, 200001, 0, y2);
  }
  scanf("%d", &T);
  while (T--) {
    int l, r, x;
    scanf("%d%d%d", &l, &r, &x);
    x = (ans + x) % 1000000000;
    x = min(x, 200001);
    A = B = 0;
    query(root[r], 0, 200001, x);
    ans = A * x + B;
    A = B = 0;
    query(root[l - 1], 0, 200001, x);
    ans -= A * x + B;
    printf("%I64d\n", ans);
  }
  return 0;
}

```