#include <bits/stdc++.h>
using namespace std;
const int N = 75000 + 10;
const int A = 2e5 + 1;
const int BASE = 1e9;
int n, q;
int Root[2][N];
long long SA[N], SB[N];
struct Node {
  long long v, SA, SB;
  Node() {}
  Node(long long v, long long SA, long long SB) : v(v), SA(SA), SB(SB) {}
  Node operator+(const Node &that) const {
    return Node(v + that.v, SA + that.SA, SB + that.SB);
  }
  Node operator-(const Node &that) const {
    return Node(v - that.v, SA - that.SA, SB - that.SB);
  }
};
const int SZ = 20 * (A + 10);
struct PersistentST {
  int TSize;
  int L[SZ], R[SZ];
  long long S[SZ], SA[SZ], SB[SZ];
  void Init(int k) {
    TSize = 0;
    Root[k][0] = Build(0, A);
  }
  int Build(int l, int r) {
    int ID = ++TSize;
    if (l == r) return ID;
    L[ID] = Build(l, ((l + r) >> 1));
    R[ID] = Build(((l + r) >> 1) + 1, r);
    S[ID] = SA[ID] = SB[ID] = 0;
    return ID;
  }
  int Update(int id, int l, int r, int i, long long v, long long a,
             long long b) {
    if (l > i || r < i) return id;
    int ID = ++TSize;
    if (l == r) {
      S[ID] = S[id] + v;
      SA[ID] = SA[id] + a;
      SB[ID] = SB[id] + b;
      return ID;
    }
    L[ID] = Update(L[id], l, ((l + r) >> 1), i, v, a, b);
    R[ID] = Update(R[id], ((l + r) >> 1) + 1, r, i, v, a, b);
    S[ID] = S[L[ID]] + S[R[ID]];
    SA[ID] = SA[L[ID]] + SA[R[ID]];
    SB[ID] = SB[L[ID]] + SB[R[ID]];
    return ID;
  }
  Node Query(int ID, int l, int r, int i, int j) {
    if (l > j || r < i) return Node(0, 0, 0);
    if (i <= l && r <= j) return Node(S[ID], SA[ID], SB[ID]);
    return Query(L[ID], l, ((l + r) >> 1), i, j) +
           Query(R[ID], ((l + r) >> 1) + 1, r, i, j);
  }
} PST[2];
Node QueryTree(int k, int l, int r, int i, int j) {
  if (j - i + 1 <= 0) return Node(0, 0, 0);
  return PST[k].Query(Root[k][r], 0, A, i, j) -
         PST[k].Query(Root[k][l - 1], 0, A, i, j);
}
long long Query(int l, int r, int x) {
  x = min(x, A);
  Node L = QueryTree(0, l, r, x, A);
  Node R = QueryTree(1, l, r, 0, x - 1);
  return L.v + R.v + (SA[r] - SA[l - 1] - L.SA - R.SA) * x +
         (SB[r] - SB[l - 1] - L.SB - R.SB);
}
int main() {
  scanf("%d", &n);
  for (int k = 0; k <= 1; ++k) PST[k].Init(k);
  for (int i = 1; i <= n; ++i) {
    int x1, x2, y1, a, b, y2;
    scanf("%d%d%d%d%d%d", &x1, &x2, &y1, &a, &b, &y2);
    SA[i] = SA[i - 1] + a;
    SB[i] = SB[i - 1] + b;
    Root[0][i] = PST[0].Update(Root[0][i - 1], 0, A, x1, y1, a, b);
    Root[1][i] = PST[1].Update(Root[1][i - 1], 0, A, x2, y2, a, b);
  }
  long long lastAns = 0;
  scanf("%d", &q);
  while (q--) {
    int l, r, x;
    scanf("%d%d%d", &l, &r, &x);
    x = (x + lastAns) % BASE;
    printf("%I64d\n", lastAns = Query(l, r, x));
  }
  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 N = 75000 + 10;
const int A = 2e5 + 1;
const int BASE = 1e9;
int n, q;
int Root[2][N];
long long SA[N], SB[N];
struct Node {
  long long v, SA, SB;
  Node() {}
  Node(long long v, long long SA, long long SB) : v(v), SA(SA), SB(SB) {}
  Node operator+(const Node &that) const {
    return Node(v + that.v, SA + that.SA, SB + that.SB);
  }
  Node operator-(const Node &that) const {
    return Node(v - that.v, SA - that.SA, SB - that.SB);
  }
};
const int SZ = 20 * (A + 10);
struct PersistentST {
  int TSize;
  int L[SZ], R[SZ];
  long long S[SZ], SA[SZ], SB[SZ];
  void Init(int k) {
    TSize = 0;
    Root[k][0] = Build(0, A);
  }
  int Build(int l, int r) {
    int ID = ++TSize;
    if (l == r) return ID;
    L[ID] = Build(l, ((l + r) >> 1));
    R[ID] = Build(((l + r) >> 1) + 1, r);
    S[ID] = SA[ID] = SB[ID] = 0;
    return ID;
  }
  int Update(int id, int l, int r, int i, long long v, long long a,
             long long b) {
    if (l > i || r < i) return id;
    int ID = ++TSize;
    if (l == r) {
      S[ID] = S[id] + v;
      SA[ID] = SA[id] + a;
      SB[ID] = SB[id] + b;
      return ID;
    }
    L[ID] = Update(L[id], l, ((l + r) >> 1), i, v, a, b);
    R[ID] = Update(R[id], ((l + r) >> 1) + 1, r, i, v, a, b);
    S[ID] = S[L[ID]] + S[R[ID]];
    SA[ID] = SA[L[ID]] + SA[R[ID]];
    SB[ID] = SB[L[ID]] + SB[R[ID]];
    return ID;
  }
  Node Query(int ID, int l, int r, int i, int j) {
    if (l > j || r < i) return Node(0, 0, 0);
    if (i <= l && r <= j) return Node(S[ID], SA[ID], SB[ID]);
    return Query(L[ID], l, ((l + r) >> 1), i, j) +
           Query(R[ID], ((l + r) >> 1) + 1, r, i, j);
  }
} PST[2];
Node QueryTree(int k, int l, int r, int i, int j) {
  if (j - i + 1 <= 0) return Node(0, 0, 0);
  return PST[k].Query(Root[k][r], 0, A, i, j) -
         PST[k].Query(Root[k][l - 1], 0, A, i, j);
}
long long Query(int l, int r, int x) {
  x = min(x, A);
  Node L = QueryTree(0, l, r, x, A);
  Node R = QueryTree(1, l, r, 0, x - 1);
  return L.v + R.v + (SA[r] - SA[l - 1] - L.SA - R.SA) * x +
         (SB[r] - SB[l - 1] - L.SB - R.SB);
}
int main() {
  scanf("%d", &n);
  for (int k = 0; k <= 1; ++k) PST[k].Init(k);
  for (int i = 1; i <= n; ++i) {
    int x1, x2, y1, a, b, y2;
    scanf("%d%d%d%d%d%d", &x1, &x2, &y1, &a, &b, &y2);
    SA[i] = SA[i - 1] + a;
    SB[i] = SB[i - 1] + b;
    Root[0][i] = PST[0].Update(Root[0][i - 1], 0, A, x1, y1, a, b);
    Root[1][i] = PST[1].Update(Root[1][i - 1], 0, A, x2, y2, a, b);
  }
  long long lastAns = 0;
  scanf("%d", &q);
  while (q--) {
    int l, r, x;
    scanf("%d%d%d", &l, &r, &x);
    x = (x + lastAns) % BASE;
    printf("%I64d\n", lastAns = Query(l, r, x));
  }
  return 0;
}

```