#include <bits/stdc++.h>
using namespace std;
const int N = 1e6 + 100;
const int M = 17;
const long long mod = 1e9;
const long long MOD = 998244353;
const long double eps = 0.000000001;
const int P = 1336;
const int inf = 1e9 + 7;
mt19937 gen(chrono::high_resolution_clock::now().time_since_epoch().count());
long long pr = 262144;
struct DSU {
  struct node {
    int l, r;
    long long sum;
  };
  vector<node> t;
  vector<int> root;
  void build() {
    t.resize(2 * pr);
    root.push_back(1);
    for (int i = 1; i < pr; i++) {
      t[i].sum = 0;
      t[i].l = i * 2;
      t[i].r = i * 2 + 1;
    }
    for (int i = pr; i < pr + pr; i++) {
      t[i].l = t[i].r = i;
    }
  }
  int upd(int pos, int v, int cl, int cr, int zn) {
    t.push_back(t[v]);
    v = t.size() - 1;
    if (cl == cr) {
      t[v].sum += zn;
      return v;
    }
    int mid = (cl + cr) / 2;
    if (pos <= mid) {
      int c = upd(pos, t[v].l, cl, mid, zn);
      t[v].l = c;
    } else {
      int c = upd(pos, t[v].r, mid + 1, cr, zn);
      t[v].r = c;
    }
    t[v].sum = t[t[v].l].sum + t[t[v].r].sum;
    return v;
  }
  long long get_sum(int nl, int nr, int v, int cl, int cr) {
    if (cr < nl || nr < cl) return 0;
    if (nl <= cl && cr <= nr) return t[v].sum;
    int mid = (cl + cr) / 2;
    return get_sum(nl, nr, t[v].l, cl, mid) +
           get_sum(nl, nr, t[v].r, mid + 1, cr);
  }
};
int32_t main() {
  ios_base::sync_with_stdio(0);
  cin.tie(0);
  cout.tie(0);
  srand(time(0));
  DSU t1, t2, t3, t4;
  t1.build();
  t2.build();
  t3.build();
  t4.build();
  int n;
  cin >> n;
  for (int i = 0; i < n; i++) {
    int x1, x2, y1, a, b, y2;
    cin >> x1 >> x2 >> y1 >> a >> b >> y2;
    t1.root.push_back(t1.upd(x1, t1.root.back(), 0, pr - 1, y1));
    t2.root.push_back(t2.upd(x2, t2.root.back(), 0, pr - 1, y2));
    int c = t3.upd(x1 + 1, t3.root.back(), 0, pr - 1, b);
    t3.root.push_back(t3.upd(x2 + 1, c, 0, pr - 1, -b));
    c = t4.upd(x1 + 1, t4.root.back(), 0, pr - 1, a);
    t4.root.push_back(t4.upd(x2 + 1, c, 0, pr - 1, -a));
  }
  int q;
  cin >> q;
  long long last = 0;
  while (q--) {
    int l, r, x;
    cin >> l >> r >> x;
    x = (1ll * x + last) % mod;
    long long ans = t1.get_sum(x, pr - 1, t1.root[r], 0, pr - 1);
    ans -= t1.get_sum(x, pr - 1, t1.root[l - 1], 0, pr - 1);
    ans += t2.get_sum(0, x - 1, t2.root[r], 0, pr - 1);
    ans -= t2.get_sum(0, x - 1, t2.root[l - 1], 0, pr - 1);
    ans += t3.get_sum(0, x, t3.root[r], 0, pr - 1);
    ans -= t3.get_sum(0, x, t3.root[l - 1], 0, pr - 1);
    long long c = t4.get_sum(0, x, t4.root[r], 0, pr - 1);
    c -= t4.get_sum(0, x, t4.root[l - 1], 0, pr - 1);
    ans += c * x;
    cout << ans << '\n';
    last = ans;
  }
}

### Prompt

In cpp, your task is to solve 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 = 1e6 + 100;
const int M = 17;
const long long mod = 1e9;
const long long MOD = 998244353;
const long double eps = 0.000000001;
const int P = 1336;
const int inf = 1e9 + 7;
mt19937 gen(chrono::high_resolution_clock::now().time_since_epoch().count());
long long pr = 262144;
struct DSU {
  struct node {
    int l, r;
    long long sum;
  };
  vector<node> t;
  vector<int> root;
  void build() {
    t.resize(2 * pr);
    root.push_back(1);
    for (int i = 1; i < pr; i++) {
      t[i].sum = 0;
      t[i].l = i * 2;
      t[i].r = i * 2 + 1;
    }
    for (int i = pr; i < pr + pr; i++) {
      t[i].l = t[i].r = i;
    }
  }
  int upd(int pos, int v, int cl, int cr, int zn) {
    t.push_back(t[v]);
    v = t.size() - 1;
    if (cl == cr) {
      t[v].sum += zn;
      return v;
    }
    int mid = (cl + cr) / 2;
    if (pos <= mid) {
      int c = upd(pos, t[v].l, cl, mid, zn);
      t[v].l = c;
    } else {
      int c = upd(pos, t[v].r, mid + 1, cr, zn);
      t[v].r = c;
    }
    t[v].sum = t[t[v].l].sum + t[t[v].r].sum;
    return v;
  }
  long long get_sum(int nl, int nr, int v, int cl, int cr) {
    if (cr < nl || nr < cl) return 0;
    if (nl <= cl && cr <= nr) return t[v].sum;
    int mid = (cl + cr) / 2;
    return get_sum(nl, nr, t[v].l, cl, mid) +
           get_sum(nl, nr, t[v].r, mid + 1, cr);
  }
};
int32_t main() {
  ios_base::sync_with_stdio(0);
  cin.tie(0);
  cout.tie(0);
  srand(time(0));
  DSU t1, t2, t3, t4;
  t1.build();
  t2.build();
  t3.build();
  t4.build();
  int n;
  cin >> n;
  for (int i = 0; i < n; i++) {
    int x1, x2, y1, a, b, y2;
    cin >> x1 >> x2 >> y1 >> a >> b >> y2;
    t1.root.push_back(t1.upd(x1, t1.root.back(), 0, pr - 1, y1));
    t2.root.push_back(t2.upd(x2, t2.root.back(), 0, pr - 1, y2));
    int c = t3.upd(x1 + 1, t3.root.back(), 0, pr - 1, b);
    t3.root.push_back(t3.upd(x2 + 1, c, 0, pr - 1, -b));
    c = t4.upd(x1 + 1, t4.root.back(), 0, pr - 1, a);
    t4.root.push_back(t4.upd(x2 + 1, c, 0, pr - 1, -a));
  }
  int q;
  cin >> q;
  long long last = 0;
  while (q--) {
    int l, r, x;
    cin >> l >> r >> x;
    x = (1ll * x + last) % mod;
    long long ans = t1.get_sum(x, pr - 1, t1.root[r], 0, pr - 1);
    ans -= t1.get_sum(x, pr - 1, t1.root[l - 1], 0, pr - 1);
    ans += t2.get_sum(0, x - 1, t2.root[r], 0, pr - 1);
    ans -= t2.get_sum(0, x - 1, t2.root[l - 1], 0, pr - 1);
    ans += t3.get_sum(0, x, t3.root[r], 0, pr - 1);
    ans -= t3.get_sum(0, x, t3.root[l - 1], 0, pr - 1);
    long long c = t4.get_sum(0, x, t4.root[r], 0, pr - 1);
    c -= t4.get_sum(0, x, t4.root[l - 1], 0, pr - 1);
    ans += c * x;
    cout << ans << '\n';
    last = ans;
  }
}

```