#include <bits/stdc++.h>
using namespace std;
using ll = int64_t;
using pll = pair<ll, ll>;
using pii = pair<int, int>;
template <class T>
pair<T, T> operator+(const pair<T, T> &a, const pair<T, T> &b) {
  return {a.first + b.first, a.second + b.second};
}
const int X = 2e5 + 5, N = 1 << 17, NC = 18 << 18;
int n, nc = 0;
int L[NC], R[NC], lo, hi;
pll val[NC];
pll sum(int v, int l = 0, int r = n) {
  if (r <= lo || hi <= l) return {0, 0};
  if (lo <= l && r <= hi) return val[v];
  int m = l + r >> 1;
  return sum(L[v], l, m) + sum(R[v], m, r);
}
int combine(int v, int l, int r) {
  L[v] = l;
  R[v] = r;
  val[v] = val[l] + val[r];
  return v;
}
pii *y;
template <class T>
int insert(int v, T second, T e, int l, int r) {
  if (second == e) return v;
  if (r - l == 1) return val[++nc] = y[l], nc;
  int m = l + r >> 1;
  T b = lower_bound(second, e, m);
  return combine(++nc, insert(L[v], second, b, l, m), insert(R[v], b, e, m, r));
}
template <class T>
void insert(int &v, T second, T e, pii a[]) {
  y = a;
  v = insert(v, second, e, 0, n);
}
pii a[N], b[N], c[N];
int build(int l = 0, int r = n) {
  if (r - l == 1) return val[++nc] = a[l], nc;
  int m = (l + r) / 2;
  return combine(++nc, build(l, m), build(m, r));
}
int t[2 * N], cmp[X];
vector<int> ord[2][X];
signed main() {
  ios::sync_with_stdio(false);
  cin.tie(nullptr);
  cin >> n;
  for (int i = 0; i < n; ++i) {
    int x1, x2;
    cin >> x1 >> x2;
    ord[0][++x1].emplace_back(i);
    ord[1][++x2].emplace_back(i);
    cmp[i << 1 | 0] = x1;
    cmp[i << 1 | 1] = x2;
    cin >> a[i].second >> b[i].first >> b[i].second >> c[i].second;
  }
  sort(cmp, cmp + 2 * n + 1);
  int second = unique(cmp, cmp + 2 * n + 1) - cmp;
  t[0] = build();
  for (int i = 1; i < second; ++i) {
    t[i] = t[i - 1];
    insert(t[i], begin(ord[0][cmp[i]]), end(ord[0][cmp[i]]), b);
    insert(t[i], begin(ord[1][cmp[i]]), end(ord[1][cmp[i]]), c);
  }
  int q;
  cin >> q;
  ll last = 0;
  while (q--) {
    int x;
    cin >> lo >> hi >> x;
    --lo;
    x = (x + last) % 1000000000;
    auto res = sum(t[upper_bound(cmp, cmp + second, x) - cmp - 1]);
    last = x * res.first + res.second;
    cout << last << '\n';
  }
}

### Prompt

Please formulate 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;
using ll = int64_t;
using pll = pair<ll, ll>;
using pii = pair<int, int>;
template <class T>
pair<T, T> operator+(const pair<T, T> &a, const pair<T, T> &b) {
  return {a.first + b.first, a.second + b.second};
}
const int X = 2e5 + 5, N = 1 << 17, NC = 18 << 18;
int n, nc = 0;
int L[NC], R[NC], lo, hi;
pll val[NC];
pll sum(int v, int l = 0, int r = n) {
  if (r <= lo || hi <= l) return {0, 0};
  if (lo <= l && r <= hi) return val[v];
  int m = l + r >> 1;
  return sum(L[v], l, m) + sum(R[v], m, r);
}
int combine(int v, int l, int r) {
  L[v] = l;
  R[v] = r;
  val[v] = val[l] + val[r];
  return v;
}
pii *y;
template <class T>
int insert(int v, T second, T e, int l, int r) {
  if (second == e) return v;
  if (r - l == 1) return val[++nc] = y[l], nc;
  int m = l + r >> 1;
  T b = lower_bound(second, e, m);
  return combine(++nc, insert(L[v], second, b, l, m), insert(R[v], b, e, m, r));
}
template <class T>
void insert(int &v, T second, T e, pii a[]) {
  y = a;
  v = insert(v, second, e, 0, n);
}
pii a[N], b[N], c[N];
int build(int l = 0, int r = n) {
  if (r - l == 1) return val[++nc] = a[l], nc;
  int m = (l + r) / 2;
  return combine(++nc, build(l, m), build(m, r));
}
int t[2 * N], cmp[X];
vector<int> ord[2][X];
signed main() {
  ios::sync_with_stdio(false);
  cin.tie(nullptr);
  cin >> n;
  for (int i = 0; i < n; ++i) {
    int x1, x2;
    cin >> x1 >> x2;
    ord[0][++x1].emplace_back(i);
    ord[1][++x2].emplace_back(i);
    cmp[i << 1 | 0] = x1;
    cmp[i << 1 | 1] = x2;
    cin >> a[i].second >> b[i].first >> b[i].second >> c[i].second;
  }
  sort(cmp, cmp + 2 * n + 1);
  int second = unique(cmp, cmp + 2 * n + 1) - cmp;
  t[0] = build();
  for (int i = 1; i < second; ++i) {
    t[i] = t[i - 1];
    insert(t[i], begin(ord[0][cmp[i]]), end(ord[0][cmp[i]]), b);
    insert(t[i], begin(ord[1][cmp[i]]), end(ord[1][cmp[i]]), c);
  }
  int q;
  cin >> q;
  ll last = 0;
  while (q--) {
    int x;
    cin >> lo >> hi >> x;
    --lo;
    x = (x + last) % 1000000000;
    auto res = sum(t[upper_bound(cmp, cmp + second, x) - cmp - 1]);
    last = x * res.first + res.second;
    cout << last << '\n';
  }
}

```