#include <bits/stdc++.h>
using namespace std;
long long n, m, idx = -1;
const int mod = 1e9;
struct node {
  int lc = 0, rc = 0;
  pair<long long, long long> val = {0, 0};
};
vector<node> pt;
void update(int v, int l, int r, int L, int R, pair<long long, long long> val) {
  if (R < L) return;
  if (l == L && r == R) {
    pt[v].val.first += val.first;
    pt[v].val.second += val.second;
    return;
  }
  int mid = (l + r) >> 1;
  if (L <= mid) {
    int x = pt[v].lc;
    pt[v].lc = ++idx;
    if (x) {
      pt.push_back(pt[x]);
    } else
      pt.push_back(pt[0]);
    update(pt[v].lc, l, mid, L, min(R, mid), val);
  }
  if (R > mid) {
    int x = pt[v].rc;
    pt[v].rc = ++idx;
    if (x != -1)
      pt.push_back(pt[x]);
    else
      pt.push_back(pt[0]);
    update(pt[v].rc, mid + 1, r, max(L, mid + 1), R, val);
  }
}
long long get(int v, int l, int r, long long pos) {
  if (!v) return 0;
  if (pt[v].lc == -1 && pt[v].rc == -1) {
    return pt[v].val.first * pos + pt[v].val.second;
  }
  int mid = (l + r) >> 1;
  if (pos <= mid) {
    return pt[v].val.first * pos + pt[v].val.second +
           get(pt[v].lc, l, mid, pos);
  }
  return get(pt[v].rc, mid + 1, r, pos) + pt[v].val.first * pos +
         pt[v].val.second;
}
int32_t main() {
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
  cin >> n;
  int root[n + 1];
  node x;
  x.lc = 0;
  x.rc = 0;
  x.val = {0, 0};
  root[0] = ++idx;
  pt.push_back(x);
  for (int i = 0; i < n; ++i) {
    long long x1, x2, y2, a, b, y1;
    cin >> x1 >> x2 >> y1 >> a >> b >> y2;
    root[i + 1] = ++idx;
    pt.push_back(pt[root[i]]);
    pt[root[i + 1]] = pt[root[i]];
    update(root[i + 1], 0, mod, 0, x1, {0, y1});
    update(root[i + 1], 0, mod, x1 + 1, x2, {a, b});
    update(root[i + 1], 0, mod, x2 + 1, mod, {0, y2});
  }
  long long last = 0;
  cin >> m;
  while (m--) {
    long long l, r, x;
    cin >> l >> r >> x;
    x = (x + last) % mod;
    long long ans = get(root[r], 0, mod, x) - get(root[l - 1], 0, mod, x);
    cout << ans << '\n';
    last = ans;
  }
  return 0;
}

### Prompt

Please create a solution in Cpp 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 n, m, idx = -1;
const int mod = 1e9;
struct node {
  int lc = 0, rc = 0;
  pair<long long, long long> val = {0, 0};
};
vector<node> pt;
void update(int v, int l, int r, int L, int R, pair<long long, long long> val) {
  if (R < L) return;
  if (l == L && r == R) {
    pt[v].val.first += val.first;
    pt[v].val.second += val.second;
    return;
  }
  int mid = (l + r) >> 1;
  if (L <= mid) {
    int x = pt[v].lc;
    pt[v].lc = ++idx;
    if (x) {
      pt.push_back(pt[x]);
    } else
      pt.push_back(pt[0]);
    update(pt[v].lc, l, mid, L, min(R, mid), val);
  }
  if (R > mid) {
    int x = pt[v].rc;
    pt[v].rc = ++idx;
    if (x != -1)
      pt.push_back(pt[x]);
    else
      pt.push_back(pt[0]);
    update(pt[v].rc, mid + 1, r, max(L, mid + 1), R, val);
  }
}
long long get(int v, int l, int r, long long pos) {
  if (!v) return 0;
  if (pt[v].lc == -1 && pt[v].rc == -1) {
    return pt[v].val.first * pos + pt[v].val.second;
  }
  int mid = (l + r) >> 1;
  if (pos <= mid) {
    return pt[v].val.first * pos + pt[v].val.second +
           get(pt[v].lc, l, mid, pos);
  }
  return get(pt[v].rc, mid + 1, r, pos) + pt[v].val.first * pos +
         pt[v].val.second;
}
int32_t main() {
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
  cin >> n;
  int root[n + 1];
  node x;
  x.lc = 0;
  x.rc = 0;
  x.val = {0, 0};
  root[0] = ++idx;
  pt.push_back(x);
  for (int i = 0; i < n; ++i) {
    long long x1, x2, y2, a, b, y1;
    cin >> x1 >> x2 >> y1 >> a >> b >> y2;
    root[i + 1] = ++idx;
    pt.push_back(pt[root[i]]);
    pt[root[i + 1]] = pt[root[i]];
    update(root[i + 1], 0, mod, 0, x1, {0, y1});
    update(root[i + 1], 0, mod, x1 + 1, x2, {a, b});
    update(root[i + 1], 0, mod, x2 + 1, mod, {0, y2});
  }
  long long last = 0;
  cin >> m;
  while (m--) {
    long long l, r, x;
    cin >> l >> r >> x;
    x = (x + last) % mod;
    long long ans = get(root[r], 0, mod, x) - get(root[l - 1], 0, mod, x);
    cout << ans << '\n';
    last = ans;
  }
  return 0;
}

```