#include <bits/stdc++.h>
using namespace std;
using ll = long long;
const int N = 2e5 + 5;
const ll mod = 1e9;
int n, q;
int initVal[N];
vector<pair<int, pair<int, int> > > queRy[N];
int nNode, rootVer[N];
struct node {
  ll a, b;
  int lef, rig;
} st[20 * N];
int build(int l, int r) {
  if (l == r) {
    int cur = ++nNode;
    st[cur] = {0, initVal[l], 0, 0};
    return cur;
  }
  int cur = ++nNode;
  int mid = (l + r) >> 1;
  int L = build(l, mid);
  int R = build(mid + 1, r);
  st[cur].lef = L;
  st[cur].rig = R;
  st[cur].a = st[L].a + st[R].a;
  st[cur].b = st[L].b + st[R].b;
  return cur;
}
int update(int old, int l, int r, int i, ll x, ll y) {
  if (l == r) {
    int cur = ++nNode;
    st[cur].a = st[old].a + x;
    st[cur].b = st[old].b + y;
    st[cur].lef = st[cur].rig = 0;
    return cur;
  }
  int cur = ++nNode;
  int mid = (l + r) >> 1;
  if (i <= mid) {
    st[cur].lef = update(st[old].lef, l, mid, i, x, y);
    st[cur].rig = st[old].rig;
  } else {
    st[cur].lef = st[old].lef;
    st[cur].rig = update(st[old].rig, mid + 1, r, i, x, y);
  }
  int L = st[cur].lef, R = st[cur].rig;
  st[cur].a = st[L].a + st[R].a;
  st[cur].b = st[L].b + st[R].b;
  return cur;
}
pair<ll, ll> get(int id, int l, int r, int i, int j) {
  if (l > j || r < i || i > j) {
    return {0, 0};
  }
  if (l >= i && r <= j) {
    return {st[id].a, st[id].b};
  }
  int mid = (l + r) >> 1;
  pair<ll, ll> tmp1 = get(st[id].lef, l, mid, i, j);
  pair<ll, ll> tmp2 = get(st[id].rig, mid + 1, r, i, j);
  return {tmp1.first + tmp2.first, tmp1.second + tmp2.second};
}
int main() {
  ios_base::sync_with_stdio(0);
  cin.tie(0);
  cin >> n;
  for (int i = 1; i <= n; i++) {
    ll x1, x2, a, b, val1, val2;
    cin >> x1 >> x2 >> val1 >> a >> b >> val2;
    initVal[i] = val1;
    queRy[x1 + 1].push_back({i, {a, b - val1}});
    queRy[x2 + 1].push_back({i, {-a, val2 - b}});
  }
  rootVer[0] = build(1, n);
  for (int i = 1; i <= 2e5 + 1; i++) {
    rootVer[i] = rootVer[i - 1];
    for (auto j : queRy[i]) {
      rootVer[i] =
          update(rootVer[i], 1, n, j.first, j.second.first, j.second.second);
    }
  }
  cin >> q;
  ll last = 0;
  while (q--) {
    ll l, r, x;
    cin >> l >> r >> x;
    x = (x + last) % mod;
    x = min(x, (ll)(2e5 + 1));
    pair<ll, ll> res = get(rootVer[x], 1, n, l, r);
    last = res.first * x + res.second;
    cout << last << "\n";
  }
}

### 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;
using ll = long long;
const int N = 2e5 + 5;
const ll mod = 1e9;
int n, q;
int initVal[N];
vector<pair<int, pair<int, int> > > queRy[N];
int nNode, rootVer[N];
struct node {
  ll a, b;
  int lef, rig;
} st[20 * N];
int build(int l, int r) {
  if (l == r) {
    int cur = ++nNode;
    st[cur] = {0, initVal[l], 0, 0};
    return cur;
  }
  int cur = ++nNode;
  int mid = (l + r) >> 1;
  int L = build(l, mid);
  int R = build(mid + 1, r);
  st[cur].lef = L;
  st[cur].rig = R;
  st[cur].a = st[L].a + st[R].a;
  st[cur].b = st[L].b + st[R].b;
  return cur;
}
int update(int old, int l, int r, int i, ll x, ll y) {
  if (l == r) {
    int cur = ++nNode;
    st[cur].a = st[old].a + x;
    st[cur].b = st[old].b + y;
    st[cur].lef = st[cur].rig = 0;
    return cur;
  }
  int cur = ++nNode;
  int mid = (l + r) >> 1;
  if (i <= mid) {
    st[cur].lef = update(st[old].lef, l, mid, i, x, y);
    st[cur].rig = st[old].rig;
  } else {
    st[cur].lef = st[old].lef;
    st[cur].rig = update(st[old].rig, mid + 1, r, i, x, y);
  }
  int L = st[cur].lef, R = st[cur].rig;
  st[cur].a = st[L].a + st[R].a;
  st[cur].b = st[L].b + st[R].b;
  return cur;
}
pair<ll, ll> get(int id, int l, int r, int i, int j) {
  if (l > j || r < i || i > j) {
    return {0, 0};
  }
  if (l >= i && r <= j) {
    return {st[id].a, st[id].b};
  }
  int mid = (l + r) >> 1;
  pair<ll, ll> tmp1 = get(st[id].lef, l, mid, i, j);
  pair<ll, ll> tmp2 = get(st[id].rig, mid + 1, r, i, j);
  return {tmp1.first + tmp2.first, tmp1.second + tmp2.second};
}
int main() {
  ios_base::sync_with_stdio(0);
  cin.tie(0);
  cin >> n;
  for (int i = 1; i <= n; i++) {
    ll x1, x2, a, b, val1, val2;
    cin >> x1 >> x2 >> val1 >> a >> b >> val2;
    initVal[i] = val1;
    queRy[x1 + 1].push_back({i, {a, b - val1}});
    queRy[x2 + 1].push_back({i, {-a, val2 - b}});
  }
  rootVer[0] = build(1, n);
  for (int i = 1; i <= 2e5 + 1; i++) {
    rootVer[i] = rootVer[i - 1];
    for (auto j : queRy[i]) {
      rootVer[i] =
          update(rootVer[i], 1, n, j.first, j.second.first, j.second.second);
    }
  }
  cin >> q;
  ll last = 0;
  while (q--) {
    ll l, r, x;
    cin >> l >> r >> x;
    x = (x + last) % mod;
    x = min(x, (ll)(2e5 + 1));
    pair<ll, ll> res = get(rootVer[x], 1, n, l, r);
    last = res.first * x + res.second;
    cout << last << "\n";
  }
}

```