#include <bits/stdc++.h>
using namespace std;
struct node {
  long long sum, sum_idx;
  node *left, *right;
  node() {
    sum = sum_idx = 0;
    left = right = this;
  }
  node(long long s, long long s_idx, node *l = new node, node *r = new node) {
    sum = s, sum_idx = s_idx;
    left = l, right = r;
  }
};
node *insert(node *root, int l, int r, long long val, int lx = 0,
             int rx = 1000000010) {
  if (lx > r || rx < l) return root;
  if (lx >= l && rx <= r)
    return new node(root->sum + val, root->sum_idx, root->left, root->right);
  int mid = (lx + rx) / 2;
  node *ln = insert(root->left, l, r, val, lx, mid);
  node *rn = insert(root->right, l, r, val, mid + 1, rx);
  return new node(root->sum, root->sum_idx, ln, rn);
}
node *insert_idx(node *root, int l, int r, long long val, int lx = 0,
                 int rx = 1000000010) {
  if (lx > r || rx < l) return root;
  if (lx >= l && rx <= r)
    return new node(root->sum, root->sum_idx + val, root->left, root->right);
  int mid = (lx + rx) / 2;
  node *ln = insert_idx(root->left, l, r, val, lx, mid);
  node *rn = insert_idx(root->right, l, r, val, mid + 1, rx);
  return new node(root->sum, root->sum_idx, ln, rn);
}
long long query(node *l_root, node *r_root, int i, long long s = 0, int lx = 0,
                int rx = 1000000010) {
  if (lx == rx)
    return (s + (r_root->sum - l_root->sum)) +
           (r_root->sum_idx - l_root->sum_idx) * i;
  int mid = (lx + rx) / 2;
  s += (r_root->sum - l_root->sum) + (r_root->sum_idx - l_root->sum_idx) * i;
  if (i <= mid) return query(l_root->left, r_root->left, i, s, lx, mid);
  return query(l_root->right, r_root->right, i, s, mid + 1, rx);
}
int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(0);
  cout.tie(0);
  long long n, m, x1, x2, y1, y2, a, b, l, r, x, last = 0;
  cin >> n;
  vector<node *> roots(n + 1);
  roots[0] = new node;
  for (int i = 1; i <= n; i++) {
    cin >> x1 >> x2 >> y1 >> a >> b >> y2;
    roots[i] = insert(roots[i - 1], 0, x1, y1);
    roots[i] = insert(roots[i], x1 + 1, x2, b);
    roots[i] = insert_idx(roots[i], x1 + 1, x2, a);
    roots[i] = insert(roots[i], x2 + 1, 1000000010, y2);
  }
  cin >> m;
  while (m--) {
    cin >> l >> r >> x;
    x = (x + last) % 1000000000;
    last = query(roots[l - 1], roots[r], x);
    cout << last << "\n";
  }
  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;
struct node {
  long long sum, sum_idx;
  node *left, *right;
  node() {
    sum = sum_idx = 0;
    left = right = this;
  }
  node(long long s, long long s_idx, node *l = new node, node *r = new node) {
    sum = s, sum_idx = s_idx;
    left = l, right = r;
  }
};
node *insert(node *root, int l, int r, long long val, int lx = 0,
             int rx = 1000000010) {
  if (lx > r || rx < l) return root;
  if (lx >= l && rx <= r)
    return new node(root->sum + val, root->sum_idx, root->left, root->right);
  int mid = (lx + rx) / 2;
  node *ln = insert(root->left, l, r, val, lx, mid);
  node *rn = insert(root->right, l, r, val, mid + 1, rx);
  return new node(root->sum, root->sum_idx, ln, rn);
}
node *insert_idx(node *root, int l, int r, long long val, int lx = 0,
                 int rx = 1000000010) {
  if (lx > r || rx < l) return root;
  if (lx >= l && rx <= r)
    return new node(root->sum, root->sum_idx + val, root->left, root->right);
  int mid = (lx + rx) / 2;
  node *ln = insert_idx(root->left, l, r, val, lx, mid);
  node *rn = insert_idx(root->right, l, r, val, mid + 1, rx);
  return new node(root->sum, root->sum_idx, ln, rn);
}
long long query(node *l_root, node *r_root, int i, long long s = 0, int lx = 0,
                int rx = 1000000010) {
  if (lx == rx)
    return (s + (r_root->sum - l_root->sum)) +
           (r_root->sum_idx - l_root->sum_idx) * i;
  int mid = (lx + rx) / 2;
  s += (r_root->sum - l_root->sum) + (r_root->sum_idx - l_root->sum_idx) * i;
  if (i <= mid) return query(l_root->left, r_root->left, i, s, lx, mid);
  return query(l_root->right, r_root->right, i, s, mid + 1, rx);
}
int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(0);
  cout.tie(0);
  long long n, m, x1, x2, y1, y2, a, b, l, r, x, last = 0;
  cin >> n;
  vector<node *> roots(n + 1);
  roots[0] = new node;
  for (int i = 1; i <= n; i++) {
    cin >> x1 >> x2 >> y1 >> a >> b >> y2;
    roots[i] = insert(roots[i - 1], 0, x1, y1);
    roots[i] = insert(roots[i], x1 + 1, x2, b);
    roots[i] = insert_idx(roots[i], x1 + 1, x2, a);
    roots[i] = insert(roots[i], x2 + 1, 1000000010, y2);
  }
  cin >> m;
  while (m--) {
    cin >> l >> r >> x;
    x = (x + last) % 1000000000;
    last = query(roots[l - 1], roots[r], x);
    cout << last << "\n";
  }
  return 0;
}

```