#include <bits/stdc++.h>
using namespace std;
template <typename T>
void dprint(T begin, T end) {
  for (auto i = begin; i != end; i++) {
    cerr << (*i) << " ";
  }
  cerr << "\n";
}
struct node {
  node *l, *r;
  long long a, b;
  node() {
    l = r = 0;
    a = b = 0;
  }
};
node *build(int tl, int tr) {
  node *v = new node();
  if (tl + 1 == tr) return v;
  int m = (tl + tr) >> 1;
  v->l = build(tl, m);
  v->r = build(m, tr);
  return v;
}
node *add(node *v, int tl, int tr, int l, int r, long long a, long long b) {
  if (r <= tl || tr <= l) return v;
  node *u = new node();
  *u = *v;
  if (l <= tl && tr <= r) {
    u->a += a;
    u->b += b;
    return u;
  }
  int m = (tl + tr) >> 1;
  u->l = add(u->l, tl, m, l, r, a, b);
  u->r = add(u->r, m, tr, l, r, a, b);
  return u;
}
long long get(node *v, int tl, int tr, long long x) {
  long long ans = v->a + v->b * x;
  if (tl + 1 == tr) return ans;
  int m = (tl + tr) >> 1;
  if (x < m)
    return get(v->l, tl, m, x) + ans;
  else
    return get(v->r, m, tr, x) + ans;
}
int n;
const int MX = 210000;
const long long MOD = 1e9;
node *cur[MX];
int main() {
  scanf("%d", &n);
  node *root = build(0, MX);
  cur[0] = root;
  for (int i = 0; i < n; ++i) {
    int x1, x2;
    long long a, b, y1, y2;
    scanf("%d%d%lld%lld%lld%lld", &x1, &x2, &y1, &a, &b, &y2);
    root = add(root, 0, MX, 0, x1 + 1, y1, 0);
    root = add(root, 0, MX, x1 + 1, x2 + 1, b, a);
    root = add(root, 0, MX, x2 + 1, MX, y2, 0);
    cur[i + 1] = root;
  }
  int q;
  scanf("%d", &q);
  long long lst = 0;
  while (q--) {
    int l, r;
    long long x;
    scanf("%d%d%lld", &l, &r, &x);
    x = (x + lst) % MOD;
    --l;
    if (x >= MX) x = MX - 1;
    lst = get(cur[r], 0, MX, x) - get(cur[l], 0, MX, x);
    printf("%lld\n", lst);
  }
  return 0;
}

### Prompt

Please provide a Cpp coded solution 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;
template <typename T>
void dprint(T begin, T end) {
  for (auto i = begin; i != end; i++) {
    cerr << (*i) << " ";
  }
  cerr << "\n";
}
struct node {
  node *l, *r;
  long long a, b;
  node() {
    l = r = 0;
    a = b = 0;
  }
};
node *build(int tl, int tr) {
  node *v = new node();
  if (tl + 1 == tr) return v;
  int m = (tl + tr) >> 1;
  v->l = build(tl, m);
  v->r = build(m, tr);
  return v;
}
node *add(node *v, int tl, int tr, int l, int r, long long a, long long b) {
  if (r <= tl || tr <= l) return v;
  node *u = new node();
  *u = *v;
  if (l <= tl && tr <= r) {
    u->a += a;
    u->b += b;
    return u;
  }
  int m = (tl + tr) >> 1;
  u->l = add(u->l, tl, m, l, r, a, b);
  u->r = add(u->r, m, tr, l, r, a, b);
  return u;
}
long long get(node *v, int tl, int tr, long long x) {
  long long ans = v->a + v->b * x;
  if (tl + 1 == tr) return ans;
  int m = (tl + tr) >> 1;
  if (x < m)
    return get(v->l, tl, m, x) + ans;
  else
    return get(v->r, m, tr, x) + ans;
}
int n;
const int MX = 210000;
const long long MOD = 1e9;
node *cur[MX];
int main() {
  scanf("%d", &n);
  node *root = build(0, MX);
  cur[0] = root;
  for (int i = 0; i < n; ++i) {
    int x1, x2;
    long long a, b, y1, y2;
    scanf("%d%d%lld%lld%lld%lld", &x1, &x2, &y1, &a, &b, &y2);
    root = add(root, 0, MX, 0, x1 + 1, y1, 0);
    root = add(root, 0, MX, x1 + 1, x2 + 1, b, a);
    root = add(root, 0, MX, x2 + 1, MX, y2, 0);
    cur[i + 1] = root;
  }
  int q;
  scanf("%d", &q);
  long long lst = 0;
  while (q--) {
    int l, r;
    long long x;
    scanf("%d%d%lld", &l, &r, &x);
    x = (x + lst) % MOD;
    --l;
    if (x >= MX) x = MX - 1;
    lst = get(cur[r], 0, MX, x) - get(cur[l], 0, MX, x);
    printf("%lld\n", lst);
  }
  return 0;
}

```