#include <bits/stdc++.h>
using namespace std;
struct funkcja {
  int x1, x2, y1, a, b, y2;
} tbl[1003000];
struct v {
  v* l;
  v* p;
  long long w;
  v(v* le, v* pr, long long ww) : l(le), p(pr), w(ww) {}
  v() : w(0) {}
  v* insert(int q, int x, int N) {
    if (N == 1) {
      return new v(l, p, w + q);
    }
    if (x >= N / 2)
      return new v(l, p->insert(q, x - N / 2, N / 2), w + q);
    else
      return new v(l->insert(q, x, N / 2), p, w + q);
  }
  long long query1(int le, int pr, int N) {
    if (N == 1) return w;
    N /= 2;
    if (le >= N && pr >= N) {
      return p->query1(le - N, pr - N, N);
    } else if (le < N && pr < N) {
      return l->query1(le, pr, N);
    } else {
      return l->query2(le, N) + p->query3(pr - N, N);
    }
  }
  long long query2(int x, int N) {
    if (N == 1) return w;
    N /= 2;
    if (x >= N)
      return p->query2(x - N, N);
    else
      return p->w + l->query2(x, N);
  }
  long long query3(int x, int N) {
    if (N == 1) return w;
    N /= 2;
    if (x < N)
      return l->query3(x, N);
    else
      return l->w + p->query3(x - N, N);
  }
};
v* ustaw(int N) {
  if (N == 1) return new v;
  return new v(ustaw(N / 2), ustaw(N / 2), 0);
}
bool f(const pair<v*, int>& a, const pair<v*, int>& b) {
  return a.second < b.second;
}
struct drzewo {
  int N = 1;
  vector<pair<v*, int>> korzenie;
  drzewo(int n) {
    while (N < n) N *= 2;
    korzenie.push_back(make_pair(ustaw(N), -1));
  }
  void insert(int x, int t, long long q) {
    korzenie.push_back(make_pair(korzenie.back().first->insert(q, x, N), t));
  }
  long long query(int l, int p, int x) {
    return korzenie[(upper_bound(korzenie.begin(), korzenie.end(),
                                 make_pair(korzenie.back().first, x), f) -
                     korzenie.begin()) -
                    1]
        .first->query1(l, p, N);
  }
};
vector<pair<pair<int, int>, int>> w;
int32_t main() {
  ios_base::sync_with_stdio(0);
  cin.tie(0);
  int n, q;
  cin >> n;
  int l, p, x;
  drzewo A(n), B(n);
  for (int i = 0; i < n; i++) {
    cin >> tbl[i].x1 >> tbl[i].x2 >> tbl[i].y1 >> tbl[i].a >> tbl[i].b >>
        tbl[i].y2;
    B.insert(i, -1, tbl[i].y1);
    w.push_back(make_pair(make_pair(tbl[i].x1 + 1, i), 0));
    w.push_back(make_pair(make_pair(tbl[i].x2 + 1, i), 1));
  }
  sort(w.begin(), w.end());
  for (int i = 0; i < w.size(); i++) {
    int j = w[i].first.second;
    if (w[i].second == 0) {
      B.insert(j, w[i].first.first, tbl[j].b - tbl[j].y1);
      A.insert(j, w[i].first.first, tbl[j].a);
    } else {
      B.insert(j, w[i].first.first, -tbl[j].b + tbl[j].y2);
      A.insert(j, w[i].first.first, -tbl[j].a);
    }
  }
  cin >> q;
  long long last = 0;
  for (int i = 0; i < q; i++) {
    cin >> l >> p >> x;
    x = (last + x) % 1000000000;
    last = A.query(l - 1, p - 1, x) * x + B.query(l - 1, p - 1, x);
    cout << last << "\n";
  }
  return 0;
}

### Prompt

Construct a cpp code solution to the problem outlined:

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 funkcja {
  int x1, x2, y1, a, b, y2;
} tbl[1003000];
struct v {
  v* l;
  v* p;
  long long w;
  v(v* le, v* pr, long long ww) : l(le), p(pr), w(ww) {}
  v() : w(0) {}
  v* insert(int q, int x, int N) {
    if (N == 1) {
      return new v(l, p, w + q);
    }
    if (x >= N / 2)
      return new v(l, p->insert(q, x - N / 2, N / 2), w + q);
    else
      return new v(l->insert(q, x, N / 2), p, w + q);
  }
  long long query1(int le, int pr, int N) {
    if (N == 1) return w;
    N /= 2;
    if (le >= N && pr >= N) {
      return p->query1(le - N, pr - N, N);
    } else if (le < N && pr < N) {
      return l->query1(le, pr, N);
    } else {
      return l->query2(le, N) + p->query3(pr - N, N);
    }
  }
  long long query2(int x, int N) {
    if (N == 1) return w;
    N /= 2;
    if (x >= N)
      return p->query2(x - N, N);
    else
      return p->w + l->query2(x, N);
  }
  long long query3(int x, int N) {
    if (N == 1) return w;
    N /= 2;
    if (x < N)
      return l->query3(x, N);
    else
      return l->w + p->query3(x - N, N);
  }
};
v* ustaw(int N) {
  if (N == 1) return new v;
  return new v(ustaw(N / 2), ustaw(N / 2), 0);
}
bool f(const pair<v*, int>& a, const pair<v*, int>& b) {
  return a.second < b.second;
}
struct drzewo {
  int N = 1;
  vector<pair<v*, int>> korzenie;
  drzewo(int n) {
    while (N < n) N *= 2;
    korzenie.push_back(make_pair(ustaw(N), -1));
  }
  void insert(int x, int t, long long q) {
    korzenie.push_back(make_pair(korzenie.back().first->insert(q, x, N), t));
  }
  long long query(int l, int p, int x) {
    return korzenie[(upper_bound(korzenie.begin(), korzenie.end(),
                                 make_pair(korzenie.back().first, x), f) -
                     korzenie.begin()) -
                    1]
        .first->query1(l, p, N);
  }
};
vector<pair<pair<int, int>, int>> w;
int32_t main() {
  ios_base::sync_with_stdio(0);
  cin.tie(0);
  int n, q;
  cin >> n;
  int l, p, x;
  drzewo A(n), B(n);
  for (int i = 0; i < n; i++) {
    cin >> tbl[i].x1 >> tbl[i].x2 >> tbl[i].y1 >> tbl[i].a >> tbl[i].b >>
        tbl[i].y2;
    B.insert(i, -1, tbl[i].y1);
    w.push_back(make_pair(make_pair(tbl[i].x1 + 1, i), 0));
    w.push_back(make_pair(make_pair(tbl[i].x2 + 1, i), 1));
  }
  sort(w.begin(), w.end());
  for (int i = 0; i < w.size(); i++) {
    int j = w[i].first.second;
    if (w[i].second == 0) {
      B.insert(j, w[i].first.first, tbl[j].b - tbl[j].y1);
      A.insert(j, w[i].first.first, tbl[j].a);
    } else {
      B.insert(j, w[i].first.first, -tbl[j].b + tbl[j].y2);
      A.insert(j, w[i].first.first, -tbl[j].a);
    }
  }
  cin >> q;
  long long last = 0;
  for (int i = 0; i < q; i++) {
    cin >> l >> p >> x;
    x = (last + x) % 1000000000;
    last = A.query(l - 1, p - 1, x) * x + B.query(l - 1, p - 1, x);
    cout << last << "\n";
  }
  return 0;
}

```