#include <bits/stdc++.h>
using namespace std;
const int N = 75005;
const int MOD = 1e9;
struct otherthing {
  int x, val;
  long long pref;
  otherthing() { x = val = pref; }
  otherthing(int _x, int _val, long long _pref) {
    x = _x;
    val = _val;
    pref = _pref;
  }
  otherthing(int _x) {
    x = _x;
    val = pref = 0;
  }
  bool operator<(const otherthing &other) const { return this->x < other.x; }
  bool operator<(const int &other) const { return this->x < other; }
};
struct node {
  vector<otherthing> Y1, A;
} aint[4 * N];
struct thing {
  int x1, x2, y1, a, b, y2;
} temp[N];
void build(int p, int l, int r) {
  if (l == r) {
    aint[p].Y1.emplace_back(1, temp[l].y1, temp[l].y1);
    aint[p].Y1.emplace_back(temp[l].x1 + 1, temp[l].b - temp[l].y1, temp[l].b);
    aint[p].Y1.emplace_back(temp[l].x2 + 1, temp[l].y2 - temp[l].b, temp[l].y2);
    aint[p].A.emplace_back(temp[l].x1, -temp[l].a, -temp[l].a);
    aint[p].A.emplace_back(temp[l].x2, temp[l].a, 0);
    return;
  }
  int m = (l + r) >> 1;
  build(2 * p, l, m);
  build(2 * p + 1, m + 1, r);
  aint[p].Y1.resize(3 * (r - l + 1));
  aint[p].A.resize((r - l + 1) << 1);
  merge(aint[2 * p].Y1.begin(), aint[2 * p].Y1.end(),
        aint[2 * p + 1].Y1.begin(), aint[2 * p + 1].Y1.end(),
        aint[p].Y1.begin());
  for (int i = 0; i < aint[p].Y1.size(); i++) {
    if (i == 0) {
      aint[p].Y1[i].pref = aint[p].Y1[i].val;
    } else {
      aint[p].Y1[i].pref = aint[p].Y1[i - 1].pref + aint[p].Y1[i].val;
    }
  }
  merge(aint[2 * p].A.begin(), aint[2 * p].A.end(), aint[2 * p + 1].A.begin(),
        aint[2 * p + 1].A.end(), aint[p].A.begin());
  for (int i = 0; i < aint[p].A.size(); i++) {
    if (i == 0) {
      aint[p].A[i].pref = aint[p].A[i].val;
    } else {
      aint[p].A[i].pref = aint[p].A[i - 1].pref + aint[p].A[i].val;
    }
  }
}
long long query(int p, int l, int r, int ql, int qr, int x) {
  if (ql > r || l > qr) {
    return 0;
  }
  if (ql <= l && r <= qr) {
    long long ans = 0;
    int pos = upper_bound(aint[p].Y1.begin(), aint[p].Y1.end(), otherthing(x)) -
              aint[p].Y1.begin() - 1;
    if (pos >= 0) {
      ans += aint[p].Y1[pos].pref;
    }
    ans += aint[p].A[aint[p].A.size() - 1].pref * x;
    pos = lower_bound(aint[p].A.begin(), aint[p].A.end(), otherthing(x)) -
          aint[p].A.begin() - 1;
    if (pos >= 0) {
      ans -= aint[p].A[pos].pref * x;
    }
    return ans;
  }
  int m = (l + r) >> 1;
  return query(2 * p, l, m, ql, qr, x) + query(2 * p + 1, m + 1, r, ql, qr, x);
}
int main() {
  int n;
  scanf("%d", &n);
  for (int i = 1; i <= n; ++i) {
    scanf("%d %d %d %d %d %d", &temp[i].x1, &temp[i].x2, &temp[i].y1,
          &temp[i].a, &temp[i].b, &temp[i].y2);
  }
  build(1, 1, n);
  int q;
  scanf("%d", &q);
  long long last = 0;
  while (q--) {
    int l, r, x;
    scanf("%d %d %d", &l, &r, &x);
    x = (x + last) % MOD;
    last = query(1, 1, n, l, r, x);
    printf("%I64d\n", last);
  }
  return 0;
}

### Prompt

Your challenge is to write 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;
const int N = 75005;
const int MOD = 1e9;
struct otherthing {
  int x, val;
  long long pref;
  otherthing() { x = val = pref; }
  otherthing(int _x, int _val, long long _pref) {
    x = _x;
    val = _val;
    pref = _pref;
  }
  otherthing(int _x) {
    x = _x;
    val = pref = 0;
  }
  bool operator<(const otherthing &other) const { return this->x < other.x; }
  bool operator<(const int &other) const { return this->x < other; }
};
struct node {
  vector<otherthing> Y1, A;
} aint[4 * N];
struct thing {
  int x1, x2, y1, a, b, y2;
} temp[N];
void build(int p, int l, int r) {
  if (l == r) {
    aint[p].Y1.emplace_back(1, temp[l].y1, temp[l].y1);
    aint[p].Y1.emplace_back(temp[l].x1 + 1, temp[l].b - temp[l].y1, temp[l].b);
    aint[p].Y1.emplace_back(temp[l].x2 + 1, temp[l].y2 - temp[l].b, temp[l].y2);
    aint[p].A.emplace_back(temp[l].x1, -temp[l].a, -temp[l].a);
    aint[p].A.emplace_back(temp[l].x2, temp[l].a, 0);
    return;
  }
  int m = (l + r) >> 1;
  build(2 * p, l, m);
  build(2 * p + 1, m + 1, r);
  aint[p].Y1.resize(3 * (r - l + 1));
  aint[p].A.resize((r - l + 1) << 1);
  merge(aint[2 * p].Y1.begin(), aint[2 * p].Y1.end(),
        aint[2 * p + 1].Y1.begin(), aint[2 * p + 1].Y1.end(),
        aint[p].Y1.begin());
  for (int i = 0; i < aint[p].Y1.size(); i++) {
    if (i == 0) {
      aint[p].Y1[i].pref = aint[p].Y1[i].val;
    } else {
      aint[p].Y1[i].pref = aint[p].Y1[i - 1].pref + aint[p].Y1[i].val;
    }
  }
  merge(aint[2 * p].A.begin(), aint[2 * p].A.end(), aint[2 * p + 1].A.begin(),
        aint[2 * p + 1].A.end(), aint[p].A.begin());
  for (int i = 0; i < aint[p].A.size(); i++) {
    if (i == 0) {
      aint[p].A[i].pref = aint[p].A[i].val;
    } else {
      aint[p].A[i].pref = aint[p].A[i - 1].pref + aint[p].A[i].val;
    }
  }
}
long long query(int p, int l, int r, int ql, int qr, int x) {
  if (ql > r || l > qr) {
    return 0;
  }
  if (ql <= l && r <= qr) {
    long long ans = 0;
    int pos = upper_bound(aint[p].Y1.begin(), aint[p].Y1.end(), otherthing(x)) -
              aint[p].Y1.begin() - 1;
    if (pos >= 0) {
      ans += aint[p].Y1[pos].pref;
    }
    ans += aint[p].A[aint[p].A.size() - 1].pref * x;
    pos = lower_bound(aint[p].A.begin(), aint[p].A.end(), otherthing(x)) -
          aint[p].A.begin() - 1;
    if (pos >= 0) {
      ans -= aint[p].A[pos].pref * x;
    }
    return ans;
  }
  int m = (l + r) >> 1;
  return query(2 * p, l, m, ql, qr, x) + query(2 * p + 1, m + 1, r, ql, qr, x);
}
int main() {
  int n;
  scanf("%d", &n);
  for (int i = 1; i <= n; ++i) {
    scanf("%d %d %d %d %d %d", &temp[i].x1, &temp[i].x2, &temp[i].y1,
          &temp[i].a, &temp[i].b, &temp[i].y2);
  }
  build(1, 1, n);
  int q;
  scanf("%d", &q);
  long long last = 0;
  while (q--) {
    int l, r, x;
    scanf("%d %d %d", &l, &r, &x);
    x = (x + last) % MOD;
    last = query(1, 1, n, l, r, x);
    printf("%I64d\n", last);
  }
  return 0;
}

```