#include <bits/stdc++.h>
using namespace std;
constexpr int inf32 = 0x3f3f3f3f;
constexpr long long inf64 = 0x3f3f3f3f3f3f3f3f;
pair<long long, long long> operator+(pair<long long, long long> a,
                                     pair<long long, long long> b) {
  return pair<long long, long long>(a.first + b.first, a.second + b.second);
}
pair<long long, long long> operator-(pair<long long, long long> a,
                                     pair<long long, long long> b) {
  return pair<long long, long long>(a.first - b.first, a.second - b.second);
}
vector<pair<long long, long long> > key(1);
vector<int> ls(1), rs(1);
int newnode(const pair<long long, long long> &v) {
  key.push_back(v);
  ls.push_back(0), rs.push_back(0);
  return (int)((key).size()) - 1;
}
int add(int u, int i, pair<long long, long long> k, int l, int r) {
  if (r <= i || i < l) return u;
  int v = newnode(key[u] + k);
  if (r - l == 1) return v;
  int vls = add(ls[u], i, k, l, (l + r >> 1));
  int vrs = add(rs[u], i, k, (l + r >> 1), r);
  ls[v] = vls, rs[v] = vrs;
  return v;
}
pair<long long, long long> get(int u, int v, int i, int l, int r) {
  if (i <= l) return pair<long long, long long>(0, 0);
  if (r <= i) return key[v] - key[u];
  return get(ls[u], ls[v], i, l, (l + r >> 1)) +
         get(rs[u], rs[v], i, (l + r >> 1), r);
}
constexpr int W = 1e9;
int main() {
  ios::sync_with_stdio(false);
  cin.tie(0), cout.tie(0);
  int n;
  cin >> n;
  vector<int> p(n + 1);
  int rt = 0;
  for (int t = 0; t < (n); ++t) {
    int l, r, first[3], second;
    cin >> l >> r >> first[0] >> second >> first[1] >> first[2], ++l, ++r;
    rt = add(rt, 0, pair<long long, long long>(first[0], 0), 0, W);
    rt = add(rt, l, pair<long long, long long>(first[1] - first[0], second), 0,
             W);
    rt = add(rt, r, pair<long long, long long>(first[2] - first[1], -second), 0,
             W);
    p[t + 1] = rt;
  }
  int nq;
  cin >> nq;
  long long ns = 0;
  while (nq--) {
    int l, r, i;
    cin >> l >> r >> i, --l, i = (ns + i) % W;
    pair<long long, long long> f = get(p[l], p[r], i + 1, 0, W);
    cout << (ns = f.first + f.second * i) << '\n';
  }
  return 0;
}

### Prompt

Construct a CPP code solution to the problem outlined:

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;
constexpr int inf32 = 0x3f3f3f3f;
constexpr long long inf64 = 0x3f3f3f3f3f3f3f3f;
pair<long long, long long> operator+(pair<long long, long long> a,
                                     pair<long long, long long> b) {
  return pair<long long, long long>(a.first + b.first, a.second + b.second);
}
pair<long long, long long> operator-(pair<long long, long long> a,
                                     pair<long long, long long> b) {
  return pair<long long, long long>(a.first - b.first, a.second - b.second);
}
vector<pair<long long, long long> > key(1);
vector<int> ls(1), rs(1);
int newnode(const pair<long long, long long> &v) {
  key.push_back(v);
  ls.push_back(0), rs.push_back(0);
  return (int)((key).size()) - 1;
}
int add(int u, int i, pair<long long, long long> k, int l, int r) {
  if (r <= i || i < l) return u;
  int v = newnode(key[u] + k);
  if (r - l == 1) return v;
  int vls = add(ls[u], i, k, l, (l + r >> 1));
  int vrs = add(rs[u], i, k, (l + r >> 1), r);
  ls[v] = vls, rs[v] = vrs;
  return v;
}
pair<long long, long long> get(int u, int v, int i, int l, int r) {
  if (i <= l) return pair<long long, long long>(0, 0);
  if (r <= i) return key[v] - key[u];
  return get(ls[u], ls[v], i, l, (l + r >> 1)) +
         get(rs[u], rs[v], i, (l + r >> 1), r);
}
constexpr int W = 1e9;
int main() {
  ios::sync_with_stdio(false);
  cin.tie(0), cout.tie(0);
  int n;
  cin >> n;
  vector<int> p(n + 1);
  int rt = 0;
  for (int t = 0; t < (n); ++t) {
    int l, r, first[3], second;
    cin >> l >> r >> first[0] >> second >> first[1] >> first[2], ++l, ++r;
    rt = add(rt, 0, pair<long long, long long>(first[0], 0), 0, W);
    rt = add(rt, l, pair<long long, long long>(first[1] - first[0], second), 0,
             W);
    rt = add(rt, r, pair<long long, long long>(first[2] - first[1], -second), 0,
             W);
    p[t + 1] = rt;
  }
  int nq;
  cin >> nq;
  long long ns = 0;
  while (nq--) {
    int l, r, i;
    cin >> l >> r >> i, --l, i = (ns + i) % W;
    pair<long long, long long> f = get(p[l], p[r], i + 1, 0, W);
    cout << (ns = f.first + f.second * i) << '\n';
  }
  return 0;
}

```