#include <bits/stdc++.h>
using namespace std;
const int MAXN = 75005;
struct tnode {
  long long sum;
  tnode *lson, *rson;
  tnode(int x = 0) { sum = x, lson = rson = nullptr; }
  void maintain() { sum = lson->sum + rson->sum; }
};
tnode* tre[MAXN][2];
tnode* newt(int l, int r) {
  if (l == r) return new tnode();
  tnode* ret = new tnode();
  int mid = (l + r) >> 1;
  ret->lson = newt(l, mid), ret->rson = newt(mid + 1, r);
  return ret;
}
int a[MAXN][2], b[MAXN][4];
void modify(tnode* lt, tnode*& rt, int l, int r, int pos, int val) {
  rt = new tnode();
  *rt = *lt;
  if (l == r) {
    rt->sum += val;
    return;
  }
  int mid = (l + r) >> 1;
  if (pos <= mid)
    modify(lt->lson, rt->lson, l, mid, pos, val);
  else
    modify(lt->rson, rt->rson, mid + 1, r, pos, val);
  rt->maintain();
}
long long query(tnode* lt, tnode* rt, int l, int r, int ql, int qr) {
  if (l > r) return 0;
  if (ql <= l && r <= qr) return rt->sum - lt->sum;
  int mid = (l + r) >> 1;
  long long ret = 0;
  if (ql <= mid) ret += query(lt->lson, rt->lson, l, mid, ql, qr);
  if (qr > mid) ret += query(lt->rson, rt->rson, mid + 1, r, ql, qr);
  return ret;
}
int N, M;
int main() {
  scanf("%d", &N);
  int siz = 0;
  for (int i = 0; i < (int)(N); ++i) {
    scanf("%d%d%d%d%d%d", &a[i][0], &a[i][1], &b[i][0], &b[i][2], &b[i][3],
          &b[i][1]);
    siz = max(siz, a[i][1] + 1);
  }
  tre[0][0] = tre[0][1] = newt(0, siz);
  for (int i = 0; i < (int)(N); ++i) {
    modify(tre[i][0], tre[i + 1][0], 0, siz, a[i][0] + 1, b[i][2]);
    modify(tre[i + 1][0], tre[i + 1][0], 0, siz, a[i][1] + 1, -b[i][2]);
    modify(tre[i][1], tre[i + 1][1], 0, siz, 0, b[i][0]);
    modify(tre[i + 1][1], tre[i + 1][1], 0, siz, a[i][0] + 1,
           b[i][3] - b[i][0]);
    modify(tre[i + 1][1], tre[i + 1][1], 0, siz, a[i][1] + 1,
           b[i][1] - b[i][3]);
  }
  scanf("%d", &M);
  long long ret = 0;
  for (int i = 0; i < (int)(M); ++i) {
    int l, r, x;
    scanf("%d%d%d", &l, &r, &x);
    x = (x + ret) % (int)1e9;
    ret = query(tre[l - 1][0], tre[r][0], 0, siz, 0, x) * x;
    ret += query(tre[l - 1][1], tre[r][1], 0, siz, 0, x);
    printf("%lld\n", ret);
  }
  return 0;
}

### Prompt

Please create a solution in Cpp 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;
const int MAXN = 75005;
struct tnode {
  long long sum;
  tnode *lson, *rson;
  tnode(int x = 0) { sum = x, lson = rson = nullptr; }
  void maintain() { sum = lson->sum + rson->sum; }
};
tnode* tre[MAXN][2];
tnode* newt(int l, int r) {
  if (l == r) return new tnode();
  tnode* ret = new tnode();
  int mid = (l + r) >> 1;
  ret->lson = newt(l, mid), ret->rson = newt(mid + 1, r);
  return ret;
}
int a[MAXN][2], b[MAXN][4];
void modify(tnode* lt, tnode*& rt, int l, int r, int pos, int val) {
  rt = new tnode();
  *rt = *lt;
  if (l == r) {
    rt->sum += val;
    return;
  }
  int mid = (l + r) >> 1;
  if (pos <= mid)
    modify(lt->lson, rt->lson, l, mid, pos, val);
  else
    modify(lt->rson, rt->rson, mid + 1, r, pos, val);
  rt->maintain();
}
long long query(tnode* lt, tnode* rt, int l, int r, int ql, int qr) {
  if (l > r) return 0;
  if (ql <= l && r <= qr) return rt->sum - lt->sum;
  int mid = (l + r) >> 1;
  long long ret = 0;
  if (ql <= mid) ret += query(lt->lson, rt->lson, l, mid, ql, qr);
  if (qr > mid) ret += query(lt->rson, rt->rson, mid + 1, r, ql, qr);
  return ret;
}
int N, M;
int main() {
  scanf("%d", &N);
  int siz = 0;
  for (int i = 0; i < (int)(N); ++i) {
    scanf("%d%d%d%d%d%d", &a[i][0], &a[i][1], &b[i][0], &b[i][2], &b[i][3],
          &b[i][1]);
    siz = max(siz, a[i][1] + 1);
  }
  tre[0][0] = tre[0][1] = newt(0, siz);
  for (int i = 0; i < (int)(N); ++i) {
    modify(tre[i][0], tre[i + 1][0], 0, siz, a[i][0] + 1, b[i][2]);
    modify(tre[i + 1][0], tre[i + 1][0], 0, siz, a[i][1] + 1, -b[i][2]);
    modify(tre[i][1], tre[i + 1][1], 0, siz, 0, b[i][0]);
    modify(tre[i + 1][1], tre[i + 1][1], 0, siz, a[i][0] + 1,
           b[i][3] - b[i][0]);
    modify(tre[i + 1][1], tre[i + 1][1], 0, siz, a[i][1] + 1,
           b[i][1] - b[i][3]);
  }
  scanf("%d", &M);
  long long ret = 0;
  for (int i = 0; i < (int)(M); ++i) {
    int l, r, x;
    scanf("%d%d%d", &l, &r, &x);
    x = (x + ret) % (int)1e9;
    ret = query(tre[l - 1][0], tre[r][0], 0, siz, 0, x) * x;
    ret += query(tre[l - 1][1], tre[r][1], 0, siz, 0, x);
    printf("%lld\n", ret);
  }
  return 0;
}

```