#include <bits/stdc++.h>
using namespace std;
const int MAXN = 1 << 17;
const long long INF = 1e9 + 10;
struct Node {
  void BuildPref() {
    sort(vec.begin(), vec.end());
    pref.resize(vec.size() + 1);
    pref[0] = {0, 0};
    for (int i = 0; i < (int)vec.size(); i++) {
      pref[i + 1].first = pref[i].first + vec[i].second.first;
      pref[i + 1].second = pref[i].second + vec[i].second.second;
    }
  }
  long long GetAns(long long x) {
    int it = lower_bound(
                 vec.begin(), vec.end(),
                 pair<long long, pair<long long, long long>>{x, {-INF, -INF}}) -
             vec.begin();
    return pref[it].first * x + pref[it].second;
  }
  vector<pair<long long, pair<long long, long long>>> vec;
  vector<pair<long long, long long>> pref;
};
Node st[MAXN * 2];
long long GetAns(int v, int l, int r, const int a, const int b, long long x) {
  if (b < l or r < a) return 0;
  if (a <= l and r <= b) return st[v].GetAns(x);
  int m = (l + r) / 2;
  return GetAns(2 * v, l, m, a, b, x) + GetAns(2 * v + 1, m + 1, r, a, b, x);
}
int main() {
  ios::sync_with_stdio(false);
  cin.tie(0);
  int N;
  cin >> N;
  long long pot = 1;
  while (pot < N) pot *= 2;
  for (int i = 0; i < N; i++) {
    long long x1, x2, y1, a, b, y2;
    cin >> x1 >> x2 >> y1 >> a >> b >> y2;
    st[pot + i].vec.push_back({-INF, {0, y1}});
    st[pot + i].vec.push_back({x1, {a, b - y1}});
    st[pot + i].vec.push_back({x2, {-a, y2 - b}});
  }
  for (int i = pot - 1; i > 0; i--) {
    st[i].vec = st[2 * i].vec;
    st[i].vec.insert(st[i].vec.end(), st[2 * i + 1].vec.begin(),
                     st[2 * i + 1].vec.end());
  }
  for (int i = 1; i < 2 * pot; i++) st[i].BuildPref();
  int m;
  cin >> m;
  long long last = 0;
  for (int tt = 0; tt < m; tt++) {
    long long l, r, x;
    cin >> l >> r >> x;
    l--, r--;
    x += last;
    x %= (long long)1e9;
    last = GetAns(1, 0, pot - 1, l, r, x);
    cout << last << "\n";
  }
}

### Prompt

Your task is to create 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;
const int MAXN = 1 << 17;
const long long INF = 1e9 + 10;
struct Node {
  void BuildPref() {
    sort(vec.begin(), vec.end());
    pref.resize(vec.size() + 1);
    pref[0] = {0, 0};
    for (int i = 0; i < (int)vec.size(); i++) {
      pref[i + 1].first = pref[i].first + vec[i].second.first;
      pref[i + 1].second = pref[i].second + vec[i].second.second;
    }
  }
  long long GetAns(long long x) {
    int it = lower_bound(
                 vec.begin(), vec.end(),
                 pair<long long, pair<long long, long long>>{x, {-INF, -INF}}) -
             vec.begin();
    return pref[it].first * x + pref[it].second;
  }
  vector<pair<long long, pair<long long, long long>>> vec;
  vector<pair<long long, long long>> pref;
};
Node st[MAXN * 2];
long long GetAns(int v, int l, int r, const int a, const int b, long long x) {
  if (b < l or r < a) return 0;
  if (a <= l and r <= b) return st[v].GetAns(x);
  int m = (l + r) / 2;
  return GetAns(2 * v, l, m, a, b, x) + GetAns(2 * v + 1, m + 1, r, a, b, x);
}
int main() {
  ios::sync_with_stdio(false);
  cin.tie(0);
  int N;
  cin >> N;
  long long pot = 1;
  while (pot < N) pot *= 2;
  for (int i = 0; i < N; i++) {
    long long x1, x2, y1, a, b, y2;
    cin >> x1 >> x2 >> y1 >> a >> b >> y2;
    st[pot + i].vec.push_back({-INF, {0, y1}});
    st[pot + i].vec.push_back({x1, {a, b - y1}});
    st[pot + i].vec.push_back({x2, {-a, y2 - b}});
  }
  for (int i = pot - 1; i > 0; i--) {
    st[i].vec = st[2 * i].vec;
    st[i].vec.insert(st[i].vec.end(), st[2 * i + 1].vec.begin(),
                     st[2 * i + 1].vec.end());
  }
  for (int i = 1; i < 2 * pot; i++) st[i].BuildPref();
  int m;
  cin >> m;
  long long last = 0;
  for (int tt = 0; tt < m; tt++) {
    long long l, r, x;
    cin >> l >> r >> x;
    l--, r--;
    x += last;
    x %= (long long)1e9;
    last = GetAns(1, 0, pot - 1, l, r, x);
    cout << last << "\n";
  }
}

```