#include <bits/stdc++.h>
using namespace std;
int n, m;
long long firstX[100100], secondX[100100], firstY[100100], secondY[100100],
    mail[100100], constant[100100];
vector<pair<int, int> > sweep[200200 + 1];
int T = 0;
int root[200200 + 1];
pair<long long, long long> seg[200200 * 40];
int leftChild[200200 * 40];
int rightChild[200200 * 40];
pair<long long, long long> operator+(const pair<long long, long long>& lhs,
                                     const pair<long long, long long>& rhs) {
  return make_pair(lhs.first + rhs.first, lhs.second + rhs.second);
}
void build(int p, int L = 1, int R = n) {
  if (L == R) {
    seg[p] = make_pair(0, firstY[L]);
    return;
  }
  leftChild[p] = T++, rightChild[p] = T++;
  int mid = (L + R) / 2;
  build(leftChild[p], L, mid);
  build(rightChild[p], mid + 1, R);
  seg[p] = seg[leftChild[p]] + seg[rightChild[p]];
}
int update(int fId, pair<long long, long long> val, int p, int L = 1,
           int R = n) {
  if (fId < L || fId > R) return p;
  int newP = T++;
  seg[newP] = seg[p], leftChild[newP] = leftChild[p],
  rightChild[newP] = rightChild[p];
  if (L == R) {
    seg[newP] = seg[newP] + val;
    return newP;
  }
  int mid = (L + R) / 2;
  leftChild[newP] = update(fId, val, leftChild[newP], L, mid);
  rightChild[newP] = update(fId, val, rightChild[newP], mid + 1, R);
  seg[newP] = seg[leftChild[newP]] + seg[rightChild[newP]];
  return newP;
}
pair<long long, long long> query(int i, int j, int p, int L = 1, int R = n) {
  if (i > R || j < L) return make_pair(0, 0);
  if (L >= i && R <= j) return seg[p];
  int mid = (L + R) / 2;
  pair<long long, long long> c1 = query(i, j, leftChild[p], L, mid);
  pair<long long, long long> c2 = query(i, j, rightChild[p], mid + 1, R);
  return c1 + c2;
}
void precalc() {
  root[0] = T++;
  build(root[0]);
  for (int i = 1; i <= 200200; i++) {
    root[i] = root[i - 1];
    for (int j = 0; j < sweep[i].size(); j++) {
      int fId = sweep[i][j].first, type = sweep[i][j].second;
      pair<long long, long long> val;
      if (type == 1)
        val = make_pair(mail[fId], -firstY[fId] + constant[fId]);
      else
        val = make_pair(-mail[fId], -constant[fId] + secondY[fId]);
      root[i] = update(fId, val, root[i]);
    }
  }
}
long long solve(long long from, long long to, long long x) {
  long long queryX = min(x, 200200 + 0LL);
  pair<long long, long long> ans = query(from, to, root[queryX]);
  return ans.first * x + ans.second;
}
int main() {
  ios::sync_with_stdio(0);
  cin.tie(0);
  cout.tie(0);
  cin >> n;
  for (int i = 1; i <= n; i++) {
    cin >> firstX[i] >> secondX[i] >> firstY[i] >> mail[i] >> constant[i] >>
        secondY[i];
    sweep[firstX[i] + 1].push_back(make_pair(i, 1));
    sweep[secondX[i] + 1].push_back(make_pair(i, 2));
  }
  precalc();
  cin >> m;
  long long lastX = 0, MOD = 1e9;
  for (int i = 0; i < m; i++) {
    long long from, to, x;
    cin >> from >> to >> x;
    x = (x + lastX) % MOD;
    cout << (lastX = solve(from, to, x)) << endl;
  }
  return 0;
}

### Prompt

Generate 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;
int n, m;
long long firstX[100100], secondX[100100], firstY[100100], secondY[100100],
    mail[100100], constant[100100];
vector<pair<int, int> > sweep[200200 + 1];
int T = 0;
int root[200200 + 1];
pair<long long, long long> seg[200200 * 40];
int leftChild[200200 * 40];
int rightChild[200200 * 40];
pair<long long, long long> operator+(const pair<long long, long long>& lhs,
                                     const pair<long long, long long>& rhs) {
  return make_pair(lhs.first + rhs.first, lhs.second + rhs.second);
}
void build(int p, int L = 1, int R = n) {
  if (L == R) {
    seg[p] = make_pair(0, firstY[L]);
    return;
  }
  leftChild[p] = T++, rightChild[p] = T++;
  int mid = (L + R) / 2;
  build(leftChild[p], L, mid);
  build(rightChild[p], mid + 1, R);
  seg[p] = seg[leftChild[p]] + seg[rightChild[p]];
}
int update(int fId, pair<long long, long long> val, int p, int L = 1,
           int R = n) {
  if (fId < L || fId > R) return p;
  int newP = T++;
  seg[newP] = seg[p], leftChild[newP] = leftChild[p],
  rightChild[newP] = rightChild[p];
  if (L == R) {
    seg[newP] = seg[newP] + val;
    return newP;
  }
  int mid = (L + R) / 2;
  leftChild[newP] = update(fId, val, leftChild[newP], L, mid);
  rightChild[newP] = update(fId, val, rightChild[newP], mid + 1, R);
  seg[newP] = seg[leftChild[newP]] + seg[rightChild[newP]];
  return newP;
}
pair<long long, long long> query(int i, int j, int p, int L = 1, int R = n) {
  if (i > R || j < L) return make_pair(0, 0);
  if (L >= i && R <= j) return seg[p];
  int mid = (L + R) / 2;
  pair<long long, long long> c1 = query(i, j, leftChild[p], L, mid);
  pair<long long, long long> c2 = query(i, j, rightChild[p], mid + 1, R);
  return c1 + c2;
}
void precalc() {
  root[0] = T++;
  build(root[0]);
  for (int i = 1; i <= 200200; i++) {
    root[i] = root[i - 1];
    for (int j = 0; j < sweep[i].size(); j++) {
      int fId = sweep[i][j].first, type = sweep[i][j].second;
      pair<long long, long long> val;
      if (type == 1)
        val = make_pair(mail[fId], -firstY[fId] + constant[fId]);
      else
        val = make_pair(-mail[fId], -constant[fId] + secondY[fId]);
      root[i] = update(fId, val, root[i]);
    }
  }
}
long long solve(long long from, long long to, long long x) {
  long long queryX = min(x, 200200 + 0LL);
  pair<long long, long long> ans = query(from, to, root[queryX]);
  return ans.first * x + ans.second;
}
int main() {
  ios::sync_with_stdio(0);
  cin.tie(0);
  cout.tie(0);
  cin >> n;
  for (int i = 1; i <= n; i++) {
    cin >> firstX[i] >> secondX[i] >> firstY[i] >> mail[i] >> constant[i] >>
        secondY[i];
    sweep[firstX[i] + 1].push_back(make_pair(i, 1));
    sweep[secondX[i] + 1].push_back(make_pair(i, 2));
  }
  precalc();
  cin >> m;
  long long lastX = 0, MOD = 1e9;
  for (int i = 0; i < m; i++) {
    long long from, to, x;
    cin >> from >> to >> x;
    x = (x + lastX) % MOD;
    cout << (lastX = solve(from, to, x)) << endl;
  }
  return 0;
}

```