#include <bits/stdc++.h>
using namespace std;
const int maxn = 7.5e4 + 5, maxc = 1 << 18;
struct node {
  int l, r;
  long long a, b;
  node(int L = -1, int R = -1, long long A = 0, long long B = 0)
      : l(L), r(R), a(A), b(B) {}
} t[maxn * 100];
struct fun {
  int x1, x2;
  long long y1, a, b, y2;
};
int N = 0;
int build(int l, int r) {
  if (l == r) return N++;
  int vr = N++;
  t[vr].l = build(l, (l + r) / 2), t[vr].r = build((l + r) / 2 + 1, r);
  return vr;
}
int update(int l, int r, int vr, fun f) {
  if (r < f.x1 || (f.x1 <= l && r <= f.x2) || f.x2 < l) {
    t[N] = t[vr];
    if (r < f.x1) t[N].b += f.y1;
    if (f.x1 <= l && r <= f.x2) t[N].a += f.a, t[N].b += f.b;
    if (f.x2 < l) t[N].b += f.y2;
    return N++;
  }
  int nw = N++;
  t[nw] = t[vr];
  t[nw].l = update(l, (l + r) / 2, t[vr].l, f),
  t[nw].r = update((l + r) / 2 + 1, r, t[vr].r, f);
  return nw;
}
long long query(int l, int r, long long x, int vr) {
  long long ans = t[vr].a * x + t[vr].b;
  if (l != r && x <= (l + r) / 2) ans += query(l, (l + r) / 2, x, t[vr].l);
  if (l != r && x > (l + r) / 2) ans += query((l + r) / 2 + 1, r, x, t[vr].r);
  return ans;
}
int main() {
  ios::sync_with_stdio(false);
  cin.tie(0);
  vector<int> roots;
  roots.push_back(build(0, maxc - 1));
  int n;
  cin >> n;
  for (int i = 1; i <= n; i++) {
    fun f;
    cin >> f.x1 >> f.x2 >> f.y1 >> f.a >> f.b >> f.y2;
    f.x1++;
    roots.push_back(update(0, maxc - 1, roots.back(), f));
  }
  int q;
  long long ans = 0, mod = 1e9;
  cin >> q;
  while (q--) {
    int l, r, x;
    cin >> l >> r >> x;
    x = ((long long)x + ans) % mod;
    ans = query(0, maxc - 1, x, roots[r]) - query(0, maxc - 1, x, roots[l - 1]);
    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 maxn = 7.5e4 + 5, maxc = 1 << 18;
struct node {
  int l, r;
  long long a, b;
  node(int L = -1, int R = -1, long long A = 0, long long B = 0)
      : l(L), r(R), a(A), b(B) {}
} t[maxn * 100];
struct fun {
  int x1, x2;
  long long y1, a, b, y2;
};
int N = 0;
int build(int l, int r) {
  if (l == r) return N++;
  int vr = N++;
  t[vr].l = build(l, (l + r) / 2), t[vr].r = build((l + r) / 2 + 1, r);
  return vr;
}
int update(int l, int r, int vr, fun f) {
  if (r < f.x1 || (f.x1 <= l && r <= f.x2) || f.x2 < l) {
    t[N] = t[vr];
    if (r < f.x1) t[N].b += f.y1;
    if (f.x1 <= l && r <= f.x2) t[N].a += f.a, t[N].b += f.b;
    if (f.x2 < l) t[N].b += f.y2;
    return N++;
  }
  int nw = N++;
  t[nw] = t[vr];
  t[nw].l = update(l, (l + r) / 2, t[vr].l, f),
  t[nw].r = update((l + r) / 2 + 1, r, t[vr].r, f);
  return nw;
}
long long query(int l, int r, long long x, int vr) {
  long long ans = t[vr].a * x + t[vr].b;
  if (l != r && x <= (l + r) / 2) ans += query(l, (l + r) / 2, x, t[vr].l);
  if (l != r && x > (l + r) / 2) ans += query((l + r) / 2 + 1, r, x, t[vr].r);
  return ans;
}
int main() {
  ios::sync_with_stdio(false);
  cin.tie(0);
  vector<int> roots;
  roots.push_back(build(0, maxc - 1));
  int n;
  cin >> n;
  for (int i = 1; i <= n; i++) {
    fun f;
    cin >> f.x1 >> f.x2 >> f.y1 >> f.a >> f.b >> f.y2;
    f.x1++;
    roots.push_back(update(0, maxc - 1, roots.back(), f));
  }
  int q;
  long long ans = 0, mod = 1e9;
  cin >> q;
  while (q--) {
    int l, r, x;
    cin >> l >> r >> x;
    x = ((long long)x + ans) % mod;
    ans = query(0, maxc - 1, x, roots[r]) - query(0, maxc - 1, x, roots[l - 1]);
    cout << ans << "\n";
  }
  return 0;
}

```