#include <bits/stdc++.h>
using namespace std;
template <typename T>
void cmax(T& x, T y) {
  if (x < y) x = y;
}
template <typename T>
void cmin(T& x, T y) {
  if (x > y) x = y;
}
inline int read() {
  int f = 1, x = 0;
  char ch = getchar();
  while (ch > '9' || ch < '0') {
    if (ch == '-') f = -1;
    ch = getchar();
  }
  while (ch >= '0' && ch <= '9') {
    x = x * 10 + ch - '0';
    ch = getchar();
  }
  return f * x;
}
const int N = 8e6, M = 2e5 + 1;
int n, m, tot, ls[N], rs[N], T[N];
long long t1[N], t2[N], p[N];
inline void add(int& rt, int lst, int l, int r, int L, int R, int a, int b) {
  rt = ++tot;
  ls[rt] = ls[lst], rs[rt] = rs[lst], t1[rt] = t1[lst], t2[rt] = t2[lst];
  if (L <= l && r <= R) {
    t1[rt] += a, t2[rt] += b;
    return;
  }
  int mid = (l + r) >> 1;
  if (L <= mid) add(ls[rt], ls[lst], l, mid, L, R, a, b);
  if (mid < R) add(rs[rt], rs[lst], mid + 1, r, L, R, a, b);
}
inline long long query(int rt, int l, int r, int k) {
  if (l == r) return k * t1[rt] + t2[rt];
  int mid = (l + r) >> 1;
  if (mid >= k) return query(ls[rt], l, mid, k) + k * t1[rt] + t2[rt];
  return query(rs[rt], mid + 1, r, k) + k * t1[rt] + t2[rt];
}
int main() {
  n = read();
  for (int i = (1), iend = (n); i <= iend; i++) {
    int xa = read(), xb = read(), ya = read(), a = read(), b = read(),
        yb = read();
    add(T[i], T[i - 1], 0, M, 0, xa, 0, ya);
    add(T[i], T[i], 0, M, xa + 1, xb, a, b);
    add(T[i], T[i], 0, M, xb + 1, M, 0, yb);
    p[i] = p[i - 1] + yb;
  }
  m = read();
  long long lst = 0;
  for (int i = (1), iend = (m); i <= iend; i++) {
    long long l = read(), r = read(), x = read();
    x = (x + lst) % 1000000000;
    if (x > M) x = M;
    printf("%lld\n", lst = (query(T[r], 0, M, x) - query(T[l - 1], 0, M, x)));
  }
}

### 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;
template <typename T>
void cmax(T& x, T y) {
  if (x < y) x = y;
}
template <typename T>
void cmin(T& x, T y) {
  if (x > y) x = y;
}
inline int read() {
  int f = 1, x = 0;
  char ch = getchar();
  while (ch > '9' || ch < '0') {
    if (ch == '-') f = -1;
    ch = getchar();
  }
  while (ch >= '0' && ch <= '9') {
    x = x * 10 + ch - '0';
    ch = getchar();
  }
  return f * x;
}
const int N = 8e6, M = 2e5 + 1;
int n, m, tot, ls[N], rs[N], T[N];
long long t1[N], t2[N], p[N];
inline void add(int& rt, int lst, int l, int r, int L, int R, int a, int b) {
  rt = ++tot;
  ls[rt] = ls[lst], rs[rt] = rs[lst], t1[rt] = t1[lst], t2[rt] = t2[lst];
  if (L <= l && r <= R) {
    t1[rt] += a, t2[rt] += b;
    return;
  }
  int mid = (l + r) >> 1;
  if (L <= mid) add(ls[rt], ls[lst], l, mid, L, R, a, b);
  if (mid < R) add(rs[rt], rs[lst], mid + 1, r, L, R, a, b);
}
inline long long query(int rt, int l, int r, int k) {
  if (l == r) return k * t1[rt] + t2[rt];
  int mid = (l + r) >> 1;
  if (mid >= k) return query(ls[rt], l, mid, k) + k * t1[rt] + t2[rt];
  return query(rs[rt], mid + 1, r, k) + k * t1[rt] + t2[rt];
}
int main() {
  n = read();
  for (int i = (1), iend = (n); i <= iend; i++) {
    int xa = read(), xb = read(), ya = read(), a = read(), b = read(),
        yb = read();
    add(T[i], T[i - 1], 0, M, 0, xa, 0, ya);
    add(T[i], T[i], 0, M, xa + 1, xb, a, b);
    add(T[i], T[i], 0, M, xb + 1, M, 0, yb);
    p[i] = p[i - 1] + yb;
  }
  m = read();
  long long lst = 0;
  for (int i = (1), iend = (m); i <= iend; i++) {
    long long l = read(), r = read(), x = read();
    x = (x + lst) % 1000000000;
    if (x > M) x = M;
    printf("%lld\n", lst = (query(T[r], 0, M, x) - query(T[l - 1], 0, M, x)));
  }
}

```