#include <bits/stdc++.h>
using namespace std;
const int M = 80010;
const int N = 40 * M;
long long mod = 1e9 + 7;
long long fmod(long long b, long long exp) {
  long long res = 1;
  while (exp) {
    if (exp & 1ll) res = (res * b) % mod;
    b = (b * b) % mod;
    exp /= 2ll;
  }
  return res;
}
int A[M], B[M], root1[M], root2[M], root3[M], root4[M];
long long pre_y2[M];
struct pers {
  int lft[N], rt[N];
  long long tr[N];
  int len;
  pers() { len = 0; };
  int make_node(int i) {
    len++;
    tr[len] = tr[i];
    lft[len] = lft[i];
    rt[len] = rt[i];
    return len;
  }
  long long query(int r1, int r2, int st, int en, int k) {
    if (k <= st) return tr[r2] - tr[r1];
    if (k > en) return 0;
    int mid = (st + en) >> 1;
    return query(lft[r1], lft[r2], st, mid, k) +
           query(rt[r1], rt[r2], mid + 1, en, k);
  }
  int upd(int i, int st, int en, int val, int el) {
    if (val < st || en < val) return i;
    int tp = make_node(i);
    if (st == en) {
      tr[tp] += 1ll * el;
      return tp;
    }
    int mid = (st + en) >> 1;
    if (val <= mid) {
      lft[tp] = upd(lft[i], st, mid, val, el);
    } else
      rt[tp] = upd(rt[i], mid + 1, en, val, el);
    tr[tp] = (tr[lft[tp]] + tr[rt[tp]]);
    return tp;
  }
} tr_y1, tr_y2, tr_a1, tr_a2;
int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
  int t = 1, n, i, j, m;
  cin >> n;
  int x1, x2, y1, y2, a, b, l, r, x;
  long long lim = 1e9;
  long long last = 0;
  for (i = 1; i <= n; i++) {
    cin >> x1 >> x2 >> y1 >> a >> b >> y2;
    x1++;
    x2++;
    root1[i] = tr_y1.upd(root1[i - 1], 0, lim, x1, y1 - b);
    root2[i] = tr_y2.upd(root2[i - 1], 0, lim, x2, -y2 + b);
    root3[i] = tr_a1.upd(root3[i - 1], 0, lim, x1, a);
    root4[i] = tr_a2.upd(root4[i - 1], 0, lim, x2, a);
    pre_y2[i] = pre_y2[i - 1] + y2;
  }
  long long ans = 0;
  cin >> m;
  for (i = 0; i < m; i++) {
    cin >> l >> r >> x;
    x = (x + last) % lim;
    x++;
    long long ans = pre_y2[r] - pre_y2[l - 1];
    long long cur1 = tr_y1.query(root1[l - 1], root1[r], 0, lim, x) +
                     tr_y2.query(root2[l - 1], root2[r], 0, lim, x);
    long long cur2 = tr_a2.query(root4[l - 1], root4[r], 0, lim, x) -
                     tr_a1.query(root3[l - 1], root3[r], 0, lim, x);
    x--;
    ans = (ans + cur1 + cur2 * 1ll * x);
    last = ans;
    cout << ans << "\n";
  }
  return 0;
}

### 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>
using namespace std;
const int M = 80010;
const int N = 40 * M;
long long mod = 1e9 + 7;
long long fmod(long long b, long long exp) {
  long long res = 1;
  while (exp) {
    if (exp & 1ll) res = (res * b) % mod;
    b = (b * b) % mod;
    exp /= 2ll;
  }
  return res;
}
int A[M], B[M], root1[M], root2[M], root3[M], root4[M];
long long pre_y2[M];
struct pers {
  int lft[N], rt[N];
  long long tr[N];
  int len;
  pers() { len = 0; };
  int make_node(int i) {
    len++;
    tr[len] = tr[i];
    lft[len] = lft[i];
    rt[len] = rt[i];
    return len;
  }
  long long query(int r1, int r2, int st, int en, int k) {
    if (k <= st) return tr[r2] - tr[r1];
    if (k > en) return 0;
    int mid = (st + en) >> 1;
    return query(lft[r1], lft[r2], st, mid, k) +
           query(rt[r1], rt[r2], mid + 1, en, k);
  }
  int upd(int i, int st, int en, int val, int el) {
    if (val < st || en < val) return i;
    int tp = make_node(i);
    if (st == en) {
      tr[tp] += 1ll * el;
      return tp;
    }
    int mid = (st + en) >> 1;
    if (val <= mid) {
      lft[tp] = upd(lft[i], st, mid, val, el);
    } else
      rt[tp] = upd(rt[i], mid + 1, en, val, el);
    tr[tp] = (tr[lft[tp]] + tr[rt[tp]]);
    return tp;
  }
} tr_y1, tr_y2, tr_a1, tr_a2;
int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
  int t = 1, n, i, j, m;
  cin >> n;
  int x1, x2, y1, y2, a, b, l, r, x;
  long long lim = 1e9;
  long long last = 0;
  for (i = 1; i <= n; i++) {
    cin >> x1 >> x2 >> y1 >> a >> b >> y2;
    x1++;
    x2++;
    root1[i] = tr_y1.upd(root1[i - 1], 0, lim, x1, y1 - b);
    root2[i] = tr_y2.upd(root2[i - 1], 0, lim, x2, -y2 + b);
    root3[i] = tr_a1.upd(root3[i - 1], 0, lim, x1, a);
    root4[i] = tr_a2.upd(root4[i - 1], 0, lim, x2, a);
    pre_y2[i] = pre_y2[i - 1] + y2;
  }
  long long ans = 0;
  cin >> m;
  for (i = 0; i < m; i++) {
    cin >> l >> r >> x;
    x = (x + last) % lim;
    x++;
    long long ans = pre_y2[r] - pre_y2[l - 1];
    long long cur1 = tr_y1.query(root1[l - 1], root1[r], 0, lim, x) +
                     tr_y2.query(root2[l - 1], root2[r], 0, lim, x);
    long long cur2 = tr_a2.query(root4[l - 1], root4[r], 0, lim, x) -
                     tr_a1.query(root3[l - 1], root3[r], 0, lim, x);
    x--;
    ans = (ans + cur1 + cur2 * 1ll * x);
    last = ans;
    cout << ans << "\n";
  }
  return 0;
}

```