#include <bits/stdc++.h>
using namespace std;
struct func {
  long long a, b, end;
  bool operator<(const func& other) const { return end < other.end; }
  long long operator()(long long x) const { return a * x + b; }
  func() {}
  func(long long a, long long b, long long end) : a(a), b(b), end(end) {}
};
void calc_second(pair<vector<func>, vector<func>>& tmp) {
  tmp.second = tmp.first;
  for (int i = 1; i < tmp.second.size(); i++) {
    tmp.second[i].a += tmp.second[i - 1].a;
    tmp.second[i].b += tmp.second[i - 1].b;
  }
}
pair<vector<func>, vector<func>> spoji(
    const pair<vector<func>, vector<func>>& a,
    const pair<vector<func>, vector<func>>& b) {
  pair<vector<func>, vector<func>> tmp;
  merge(a.first.begin(), a.first.end(), b.first.begin(), b.first.end(),
        back_inserter(tmp.first));
  calc_second(tmp);
  return tmp;
}
const int MAXN = 131072;
struct tree {
  pair<vector<func>, vector<func>> a[2 * MAXN];
  void init() {
    for (int i = MAXN - 1; i >= 1; i--) {
      a[i] = spoji(a[2 * i], a[2 * i + 1]);
    }
  }
  long long answer(const pair<vector<func>, vector<func>>& b, long long x) {
    func t;
    t.end = x;
    auto it = lower_bound(b.second.begin(), b.second.end(), t);
    long long z = (*b.second.rbegin())(x);
    if (it != b.second.begin()) {
      --it;
      z -= (*it)(x);
    }
    return z;
  }
  long long answer(int l, int r, long long x, int node = 1, int nl = 1,
                   int nr = MAXN) {
    if (r < nl || nr < l) {
      return 0;
    }
    if (l <= nl && nr <= r) {
      return answer(a[node], x);
    }
    int m = (nl + nr) / 2;
    return answer(l, r, x, 2 * node, nl, m) +
           answer(l, r, x, 2 * node + 1, m + 1, nr);
  }
};
tree drvo;
int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(nullptr);
  cout.tie(nullptr);
  cerr.tie(nullptr);
  int n, q;
  cin >> n;
  for (int i = 1; i <= n; i++) {
    long long x1, x2, y1, a, b, y2;
    cin >> x1 >> x2 >> y1 >> a >> b >> y2;
    pair<vector<func>, vector<func>> meh;
    meh.first.push_back(func(0, y1, x1));
    meh.first.push_back(func(-a, -b, x1));
    meh.first.push_back(func(a, b, x2));
    meh.first.push_back(func(0, -y2, x2));
    meh.first.push_back(func(0, y2, 2000000000));
    calc_second(meh);
    drvo.a[i + MAXN - 1] = meh;
  }
  drvo.init();
  cin >> q;
  long long last = 0;
  while (q--) {
    int l, r, x;
    cin >> l >> r >> x;
    x = (x + last) % 1000000000;
    last = drvo.answer(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;
struct func {
  long long a, b, end;
  bool operator<(const func& other) const { return end < other.end; }
  long long operator()(long long x) const { return a * x + b; }
  func() {}
  func(long long a, long long b, long long end) : a(a), b(b), end(end) {}
};
void calc_second(pair<vector<func>, vector<func>>& tmp) {
  tmp.second = tmp.first;
  for (int i = 1; i < tmp.second.size(); i++) {
    tmp.second[i].a += tmp.second[i - 1].a;
    tmp.second[i].b += tmp.second[i - 1].b;
  }
}
pair<vector<func>, vector<func>> spoji(
    const pair<vector<func>, vector<func>>& a,
    const pair<vector<func>, vector<func>>& b) {
  pair<vector<func>, vector<func>> tmp;
  merge(a.first.begin(), a.first.end(), b.first.begin(), b.first.end(),
        back_inserter(tmp.first));
  calc_second(tmp);
  return tmp;
}
const int MAXN = 131072;
struct tree {
  pair<vector<func>, vector<func>> a[2 * MAXN];
  void init() {
    for (int i = MAXN - 1; i >= 1; i--) {
      a[i] = spoji(a[2 * i], a[2 * i + 1]);
    }
  }
  long long answer(const pair<vector<func>, vector<func>>& b, long long x) {
    func t;
    t.end = x;
    auto it = lower_bound(b.second.begin(), b.second.end(), t);
    long long z = (*b.second.rbegin())(x);
    if (it != b.second.begin()) {
      --it;
      z -= (*it)(x);
    }
    return z;
  }
  long long answer(int l, int r, long long x, int node = 1, int nl = 1,
                   int nr = MAXN) {
    if (r < nl || nr < l) {
      return 0;
    }
    if (l <= nl && nr <= r) {
      return answer(a[node], x);
    }
    int m = (nl + nr) / 2;
    return answer(l, r, x, 2 * node, nl, m) +
           answer(l, r, x, 2 * node + 1, m + 1, nr);
  }
};
tree drvo;
int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(nullptr);
  cout.tie(nullptr);
  cerr.tie(nullptr);
  int n, q;
  cin >> n;
  for (int i = 1; i <= n; i++) {
    long long x1, x2, y1, a, b, y2;
    cin >> x1 >> x2 >> y1 >> a >> b >> y2;
    pair<vector<func>, vector<func>> meh;
    meh.first.push_back(func(0, y1, x1));
    meh.first.push_back(func(-a, -b, x1));
    meh.first.push_back(func(a, b, x2));
    meh.first.push_back(func(0, -y2, x2));
    meh.first.push_back(func(0, y2, 2000000000));
    calc_second(meh);
    drvo.a[i + MAXN - 1] = meh;
  }
  drvo.init();
  cin >> q;
  long long last = 0;
  while (q--) {
    int l, r, x;
    cin >> l >> r >> x;
    x = (x + last) % 1000000000;
    last = drvo.answer(l, r, x);
    cout << last << '\n';
  }
}

```