#include <bits/stdc++.h>
using namespace std;
const int mod = 1e9 + 7;
int pwr(int a, int b) {
  int ans = 1;
  while (b) {
    if (b & 1) ans = (ans * 1LL * a) % mod;
    a = (a * 1LL * a) % mod;
    b >>= 1;
  }
  return ans;
}
const int Q = 75005 * 3;
const int LOGN = 31;
template <typename T, typename Z>
class Persistent_Segtree_Point_Updates {
  struct node {
    Z val;
    int lc, rc;
    node(Z k = {0, 0}) : lc(0), rc(0), val(k) {}
  };
  T st, en;
  Z Id;
  vector<node> tr;
  vector<int> version;
  int SZ;
  int time;
  inline int newNode() { return SZ++; }
  inline Z op(Z a, Z b) { return {a.first + b.first, a.second + b.second}; }
  void upd(int &cur, int prev, T s, T e, T idx, Z val) {
    if (!cur) {
      cur = newNode();
      tr[cur].val = tr[prev].val;
    }
    if (s == e) {
      tr[cur].val = op(tr[cur].val, val);
      return;
    }
    T m = (s + e) >> 1;
    if (idx <= m) {
      upd(tr[cur].lc, tr[prev].lc, s, m, idx, val);
      if (!tr[cur].rc) tr[cur].rc = tr[prev].rc;
    } else {
      upd(tr[cur].rc, tr[prev].rc, m + 1, e, idx, val);
      if (!tr[cur].lc) tr[cur].lc = tr[prev].lc;
    }
    tr[cur].val = op(tr[tr[cur].lc].val, tr[tr[cur].rc].val);
  }
  Z query(int cur, T s, T e, T l, T r) {
    if (s > r || e < l || !cur) return Id;
    if (l <= s && e <= r) return tr[cur].val;
    T m = (s + e) >> 1;
    return op(query(tr[cur].lc, s, m, l, r), query(tr[cur].rc, m + 1, e, l, r));
  }

 public:
  Persistent_Segtree_Point_Updates(T st, T en)
      : st(st), en(en), tr(Q * LOGN), SZ(1), time(-1), Id({0, 0}) {
    newTime();
  }
  void newTime() {
    time++;
    version.emplace_back(0);
  }
  void upd(T idx, Z val) {
    newTime();
    assert(st <= idx && idx <= en && time > 0);
    upd(version[time], version[time - 1], st, en, idx, val);
  }
  Z query(T l, T r, int tt) {
    if (l > r) return Id;
    assert(st <= l && r <= en);
    return query(version[tt], st, en, l, r);
  }
};
const long long M = 1e9 + 5;
const int N = 1e5 + 5;
Persistent_Segtree_Point_Updates<long long, pair<long long, long long>> s(0, M);
long long x1[N], x2[N], buakdsfybva[N], a[N], b[N], y2[N];
void solve() {
  int n;
  cin >> n;
  for (int i = 1; i <= n; i++) {
    cin >> x1[i] >> x2[i] >> buakdsfybva[i] >> a[i] >> b[i] >> y2[i];
    s.upd(0, {0, buakdsfybva[i]});
    s.upd(x1[i] + 1, {a[i], b[i] - buakdsfybva[i]});
    s.upd(x2[i] + 1, {-a[i], y2[i] - b[i]});
  }
  int q;
  cin >> q;
  long long last = 0;
  while (q--) {
    long long l, r, x;
    cin >> l >> r >> x;
    x = (x + last) % ((long long)1e9);
    auto z1 = s.query(0, x, 3 * (l - 1));
    auto z2 = s.query(0, x, 3 * r);
    z2.first -= z1.first;
    z2.second -= z1.second;
    cout << (last = (z2.first * x + z2.second)) << '\n';
  }
}
int main() {
  ios ::sync_with_stdio(0);
  cin.tie(0);
  cout.tie(0);
  int t;
  t = 1;
  for (int tt = 1; tt <= t; tt++) {
    solve();
  }
  return 0;
}

### Prompt

Please provide a Cpp coded solution to the problem described below:

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 mod = 1e9 + 7;
int pwr(int a, int b) {
  int ans = 1;
  while (b) {
    if (b & 1) ans = (ans * 1LL * a) % mod;
    a = (a * 1LL * a) % mod;
    b >>= 1;
  }
  return ans;
}
const int Q = 75005 * 3;
const int LOGN = 31;
template <typename T, typename Z>
class Persistent_Segtree_Point_Updates {
  struct node {
    Z val;
    int lc, rc;
    node(Z k = {0, 0}) : lc(0), rc(0), val(k) {}
  };
  T st, en;
  Z Id;
  vector<node> tr;
  vector<int> version;
  int SZ;
  int time;
  inline int newNode() { return SZ++; }
  inline Z op(Z a, Z b) { return {a.first + b.first, a.second + b.second}; }
  void upd(int &cur, int prev, T s, T e, T idx, Z val) {
    if (!cur) {
      cur = newNode();
      tr[cur].val = tr[prev].val;
    }
    if (s == e) {
      tr[cur].val = op(tr[cur].val, val);
      return;
    }
    T m = (s + e) >> 1;
    if (idx <= m) {
      upd(tr[cur].lc, tr[prev].lc, s, m, idx, val);
      if (!tr[cur].rc) tr[cur].rc = tr[prev].rc;
    } else {
      upd(tr[cur].rc, tr[prev].rc, m + 1, e, idx, val);
      if (!tr[cur].lc) tr[cur].lc = tr[prev].lc;
    }
    tr[cur].val = op(tr[tr[cur].lc].val, tr[tr[cur].rc].val);
  }
  Z query(int cur, T s, T e, T l, T r) {
    if (s > r || e < l || !cur) return Id;
    if (l <= s && e <= r) return tr[cur].val;
    T m = (s + e) >> 1;
    return op(query(tr[cur].lc, s, m, l, r), query(tr[cur].rc, m + 1, e, l, r));
  }

 public:
  Persistent_Segtree_Point_Updates(T st, T en)
      : st(st), en(en), tr(Q * LOGN), SZ(1), time(-1), Id({0, 0}) {
    newTime();
  }
  void newTime() {
    time++;
    version.emplace_back(0);
  }
  void upd(T idx, Z val) {
    newTime();
    assert(st <= idx && idx <= en && time > 0);
    upd(version[time], version[time - 1], st, en, idx, val);
  }
  Z query(T l, T r, int tt) {
    if (l > r) return Id;
    assert(st <= l && r <= en);
    return query(version[tt], st, en, l, r);
  }
};
const long long M = 1e9 + 5;
const int N = 1e5 + 5;
Persistent_Segtree_Point_Updates<long long, pair<long long, long long>> s(0, M);
long long x1[N], x2[N], buakdsfybva[N], a[N], b[N], y2[N];
void solve() {
  int n;
  cin >> n;
  for (int i = 1; i <= n; i++) {
    cin >> x1[i] >> x2[i] >> buakdsfybva[i] >> a[i] >> b[i] >> y2[i];
    s.upd(0, {0, buakdsfybva[i]});
    s.upd(x1[i] + 1, {a[i], b[i] - buakdsfybva[i]});
    s.upd(x2[i] + 1, {-a[i], y2[i] - b[i]});
  }
  int q;
  cin >> q;
  long long last = 0;
  while (q--) {
    long long l, r, x;
    cin >> l >> r >> x;
    x = (x + last) % ((long long)1e9);
    auto z1 = s.query(0, x, 3 * (l - 1));
    auto z2 = s.query(0, x, 3 * r);
    z2.first -= z1.first;
    z2.second -= z1.second;
    cout << (last = (z2.first * x + z2.second)) << '\n';
  }
}
int main() {
  ios ::sync_with_stdio(0);
  cin.tie(0);
  cout.tie(0);
  int t;
  t = 1;
  for (int tt = 1; tt <= t; tt++) {
    solve();
  }
  return 0;
}

```