#include <bits/stdc++.h>
int inp() {
  char c = getchar();
  while (c < '0' || c > '9') c = getchar();
  int sum = 0;
  while (c >= '0' && c <= '9') {
    sum = sum * 10 + c - '0';
    c = getchar();
  }
  return sum;
}
int root[1000010];
struct President_Tree {
  int lc[10000000], rc[10000000];
  long long k[10000000], b[10000000];
  int cnt;
  void push_up(int cur) {
    k[cur] = k[lc[cur]] + k[rc[cur]];
    b[cur] = b[lc[cur]] + b[rc[cur]];
  }
  void insert(int &cur, int last, int x, int kc, int bc, int l, int r) {
    cur = ++cnt;
    if (l == r) {
      k[cur] = k[last] + kc;
      b[cur] = b[last] + bc;
      return;
    }
    int mid = (l + r) >> 1;
    if (x <= mid) {
      insert(lc[cur], lc[last], x, kc, bc, l, mid);
      rc[cur] = rc[last];
    } else {
      lc[cur] = lc[last];
      insert(rc[cur], rc[last], x, kc, bc, mid + 1, r);
    }
    push_up(cur);
  }
  long long query(int cur, int last, int ql, int qr, long long x, int l,
                  int r) {
    if (l > qr || r < ql) return 0;
    if (l >= ql && r <= qr) return x * (k[cur] - k[last]) + (b[cur] - b[last]);
    int mid = (l + r) >> 1;
    return query(lc[cur], lc[last], ql, qr, x, l, mid) +
           query(rc[cur], rc[last], ql, qr, x, mid + 1, r);
  }
} t;
int main() {
  int n = inp();
  for (int i = 1; i <= n; i++) {
    int xl = inp();
    int xr = inp();
    int yl = inp();
    int k = inp();
    int b = inp();
    int yr = inp();
    t.insert(root[i], root[i - 1], 0, 0, yl, 0, 1e9);
    t.insert(root[i], root[i], xl + 1, k, b - yl, 0, 1e9);
    t.insert(root[i], root[i], xr + 1, -k, yr - b, 0, 1e9);
  }
  int m = inp();
  long long lastans = 0;
  while (m--) {
    int l = inp();
    int r = inp();
    int x = (inp() + lastans) % 1000000000;
    lastans = t.query(root[r], root[l - 1], 0, x, x, 0, 1e9);
    printf("%lld\n", lastans);
  }
}

### Prompt

Develop a solution in CPP to the problem described below:

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>
int inp() {
  char c = getchar();
  while (c < '0' || c > '9') c = getchar();
  int sum = 0;
  while (c >= '0' && c <= '9') {
    sum = sum * 10 + c - '0';
    c = getchar();
  }
  return sum;
}
int root[1000010];
struct President_Tree {
  int lc[10000000], rc[10000000];
  long long k[10000000], b[10000000];
  int cnt;
  void push_up(int cur) {
    k[cur] = k[lc[cur]] + k[rc[cur]];
    b[cur] = b[lc[cur]] + b[rc[cur]];
  }
  void insert(int &cur, int last, int x, int kc, int bc, int l, int r) {
    cur = ++cnt;
    if (l == r) {
      k[cur] = k[last] + kc;
      b[cur] = b[last] + bc;
      return;
    }
    int mid = (l + r) >> 1;
    if (x <= mid) {
      insert(lc[cur], lc[last], x, kc, bc, l, mid);
      rc[cur] = rc[last];
    } else {
      lc[cur] = lc[last];
      insert(rc[cur], rc[last], x, kc, bc, mid + 1, r);
    }
    push_up(cur);
  }
  long long query(int cur, int last, int ql, int qr, long long x, int l,
                  int r) {
    if (l > qr || r < ql) return 0;
    if (l >= ql && r <= qr) return x * (k[cur] - k[last]) + (b[cur] - b[last]);
    int mid = (l + r) >> 1;
    return query(lc[cur], lc[last], ql, qr, x, l, mid) +
           query(rc[cur], rc[last], ql, qr, x, mid + 1, r);
  }
} t;
int main() {
  int n = inp();
  for (int i = 1; i <= n; i++) {
    int xl = inp();
    int xr = inp();
    int yl = inp();
    int k = inp();
    int b = inp();
    int yr = inp();
    t.insert(root[i], root[i - 1], 0, 0, yl, 0, 1e9);
    t.insert(root[i], root[i], xl + 1, k, b - yl, 0, 1e9);
    t.insert(root[i], root[i], xr + 1, -k, yr - b, 0, 1e9);
  }
  int m = inp();
  long long lastans = 0;
  while (m--) {
    int l = inp();
    int r = inp();
    int x = (inp() + lastans) % 1000000000;
    lastans = t.query(root[r], root[l - 1], 0, x, x, 0, 1e9);
    printf("%lld\n", lastans);
  }
}

```