#include <bits/stdc++.h>
using namespace std;
template <typename _T>
inline void _DBG(const char *s, _T x) {
  cerr << s << " = " << x << "\n";
}
template <typename _T, typename... args>
void _DBG(const char *s, _T x, args... a) {
  while (*s != ',') cerr << *s++;
  cerr << " = " << x << ',';
  _DBG(s + 1, a...);
}
template <typename TP>
struct pSegmentTree {
  long long MAX;
  TP init;
  struct node {
    TP val;
    node *left = nullptr, *right = nullptr;
    long long l, r;
    node(long long L, long long R, TP v) : val(v), l(L), r(R) {}
  };
  vector<node *> roots;
  pSegmentTree(long long n, TP zero) : init(zero) {
    MAX = 1LL << (64 - __builtin_clzll(n));
    roots.push_back(new node(0, MAX - 1, init));
  }
  void check(node *root) {
    long long m = (root->l + root->r) / 2;
    if (root->left == nullptr) root->left = new node(root->l, m, init);
    if (root->right == nullptr) root->right = new node(m + 1, root->r, init);
  }
  node *add(node *root, long long a, long long b, TP v) {
    node *res = new node(*root);
    if (res->r < a || b < res->l) return res;
    if (a <= res->l && res->r <= b) {
      res->val += v;
      return res;
    }
    check(res);
    res->left = add(res->left, a, b, v);
    res->right = add(res->right, a, b, v);
    return res;
  }
  TP ask(node *root, long long id) {
    long long m = (root->l + root->r) / 2;
    TP ans = root->val;
    if (id <= m && root->left != nullptr) ans += ask(root->left, id);
    if (id > m && root->right != nullptr) ans += ask(root->right, id);
    return ans;
  }
  void update(long long a, long long b, TP v, int state = -1) {
    if (state == -1) state += (int)(roots).size();
    assert(state < (int)(roots).size());
    roots.push_back(add(roots[state], a, b, v));
  }
  void update2(long long a, long long b, TP v) {
    roots.back() = add(roots.back(), a, b, v);
  }
  TP query(long long id, int state = -1) {
    if (state == -1) state += (int)(roots).size();
    assert(state < (int)(roots).size());
    return ask(roots[state], id);
  }
};
const int N = 2e5 + 7;
const int M = 1e9;
int n;
pSegmentTree<long long> A(N, 0), B(N, 0);
int main() {
  ios_base::sync_with_stdio(0);
  cin.tie(0);
  cout.tie(0);
  cin >> n;
  for (int i = 0; i < n; i++) {
    long long x1, x2, y1, a, b, y2;
    cin >> x1 >> x2 >> y1 >> a >> b >> y2;
    B.update(0, x1, y1);
    B.update2(x1 + 1, x2, b);
    B.update2(x2 + 1, N, y2);
    A.update(x1 + 1, x2, a);
  }
  int m;
  cin >> m;
  long long last = 0;
  while (m--) {
    long long l, r, x;
    cin >> l >> r >> x;
    x = (x + last) % M;
    x = min(x, (long long)N);
    last = B.query(x, r) - B.query(x, l - 1);
    last += x * (A.query(x, r) - A.query(x, l - 1));
    cout << last << "\n";
  }
  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;
template <typename _T>
inline void _DBG(const char *s, _T x) {
  cerr << s << " = " << x << "\n";
}
template <typename _T, typename... args>
void _DBG(const char *s, _T x, args... a) {
  while (*s != ',') cerr << *s++;
  cerr << " = " << x << ',';
  _DBG(s + 1, a...);
}
template <typename TP>
struct pSegmentTree {
  long long MAX;
  TP init;
  struct node {
    TP val;
    node *left = nullptr, *right = nullptr;
    long long l, r;
    node(long long L, long long R, TP v) : val(v), l(L), r(R) {}
  };
  vector<node *> roots;
  pSegmentTree(long long n, TP zero) : init(zero) {
    MAX = 1LL << (64 - __builtin_clzll(n));
    roots.push_back(new node(0, MAX - 1, init));
  }
  void check(node *root) {
    long long m = (root->l + root->r) / 2;
    if (root->left == nullptr) root->left = new node(root->l, m, init);
    if (root->right == nullptr) root->right = new node(m + 1, root->r, init);
  }
  node *add(node *root, long long a, long long b, TP v) {
    node *res = new node(*root);
    if (res->r < a || b < res->l) return res;
    if (a <= res->l && res->r <= b) {
      res->val += v;
      return res;
    }
    check(res);
    res->left = add(res->left, a, b, v);
    res->right = add(res->right, a, b, v);
    return res;
  }
  TP ask(node *root, long long id) {
    long long m = (root->l + root->r) / 2;
    TP ans = root->val;
    if (id <= m && root->left != nullptr) ans += ask(root->left, id);
    if (id > m && root->right != nullptr) ans += ask(root->right, id);
    return ans;
  }
  void update(long long a, long long b, TP v, int state = -1) {
    if (state == -1) state += (int)(roots).size();
    assert(state < (int)(roots).size());
    roots.push_back(add(roots[state], a, b, v));
  }
  void update2(long long a, long long b, TP v) {
    roots.back() = add(roots.back(), a, b, v);
  }
  TP query(long long id, int state = -1) {
    if (state == -1) state += (int)(roots).size();
    assert(state < (int)(roots).size());
    return ask(roots[state], id);
  }
};
const int N = 2e5 + 7;
const int M = 1e9;
int n;
pSegmentTree<long long> A(N, 0), B(N, 0);
int main() {
  ios_base::sync_with_stdio(0);
  cin.tie(0);
  cout.tie(0);
  cin >> n;
  for (int i = 0; i < n; i++) {
    long long x1, x2, y1, a, b, y2;
    cin >> x1 >> x2 >> y1 >> a >> b >> y2;
    B.update(0, x1, y1);
    B.update2(x1 + 1, x2, b);
    B.update2(x2 + 1, N, y2);
    A.update(x1 + 1, x2, a);
  }
  int m;
  cin >> m;
  long long last = 0;
  while (m--) {
    long long l, r, x;
    cin >> l >> r >> x;
    x = (x + last) % M;
    x = min(x, (long long)N);
    last = B.query(x, r) - B.query(x, l - 1);
    last += x * (A.query(x, r) - A.query(x, l - 1));
    cout << last << "\n";
  }
  return 0;
}

```