#include <bits/stdc++.h>
using namespace std;
pair<long long, long long> operator+(const pair<long long, long long>& p1,
                                     const pair<long long, long long>& p2) {
  return pair<long long, long long>(p1.first + p2.first, p1.second + p2.second);
}
struct PersistentNonLazySegmentTree {
 private:
  const pair<long long, long long> E = pair<long long, long long>(0, 0);

 private:
  static const int Nax = 100 * 100100;

 private:
  int n;

 private:
  int Counter;

 private:
  int Left[Nax];

 private:
  int Right[Nax];

 private:
  pair<long long, long long> Tree[Nax];

 private:
  vector<int> Roots;

 private:
  int NewNode() {
    Tree[Counter] = E;
    Left[Counter] = -1;
    Right[Counter] = -1;
    return Counter++;
  }

 private:
  int NewNode(int Copy) {
    Tree[Counter] = Tree[Copy];
    Left[Counter] = Left[Copy];
    Right[Counter] = Right[Copy];
    return Counter++;
  }

 private:
  pair<long long, long long> Unite(pair<long long, long long> x,
                                   pair<long long, long long> y) {
    return x + y;
  }

 private:
  void Merge(int Node) {
    Tree[Node] = Unite(Tree[Left[Node]], Tree[Right[Node]]);
  }

 public:
  int Build(const vector<pair<long long, long long>>& a) {
    Clear();
    n = a.size();
    Roots.push_back(NewNode());
    Build(a, Roots[0], 0, n - 1);
    return (int)Roots.size() - 1;
  }

 private:
  void Build(const vector<pair<long long, long long>>& a, int Node, int L,
             int R) {
    if (L == R) return void(Tree[Node] = a[L]);
    Build(a, Left[Node] = NewNode(), L, ((L + R) >> 1));
    Build(a, Right[Node] = NewNode(), ((L + R) >> 1) + 1, R);
    Merge(Node);
  }

 public:
  int Update(int i, pair<long long, long long> x) {
    Roots.push_back(Update(i, x, Roots.back(), 0, n - 1));
    return (int)Roots.size() - 1;
  }

 private:
  int Update(int i, pair<long long, long long> x, int Node, int L, int R) {
    if (i < L || R < i) return Node;
    Node = NewNode(Node);
    if (i <= L && R <= i) return Tree[Node] = x, Node;
    if (i <= ((L + R) >> 1))
      Left[Node] = Update(i, x, Left[Node], L, ((L + R) >> 1));
    else
      Right[Node] = Update(i, x, Right[Node], ((L + R) >> 1) + 1, R);
    return Merge(Node), Node;
  }

 public:
  pair<long long, long long> Query(int Version, int L, int R) {
    return Query(L, R, Roots[Version], 0, n - 1);
  }

 private:
  pair<long long, long long> Query(int i, int j, int Node, int L, int R) {
    if (i > j || L > R) return E;
    if (j < L || R < i) return E;
    if (i <= L && R <= j) return Tree[Node];
    return Unite(Query(i, j, Left[Node], L, ((L + R) >> 1)),
                 Query(i, j, Right[Node], ((L + R) >> 1) + 1, R));
  }

 public:
  void Clear() {
    n = 0;
    Counter = 0;
    Roots.clear();
  }
} myNonLazyTree;
struct Function {
  int a, b;
  int x1, y1;
  int x2, y2;
  Function(long long a = 0, long long b = 0, long long x1 = 0, long long y1 = 0,
           long long x2 = 0, long long y2 = 0)
      : a(a), b(b), x1(x1), y1(y1), x2(x2), y2(y2) {}
  void Read() { scanf("%d%d%d%d%d%d", &x1, &x2, &y1, &a, &b, &y2); }
};
int main() {
  int n;
  cin >> n;
  vector<Function> a(n);
  for (int i = 0; i < n; i++) a[i].Read();
  set<int> Axis;
  map<int, int> Version;
  map<int, vector<int>> In;
  map<int, vector<int>> Out;
  for (int i = 0; i < n; i++) {
    Axis.insert(a[i].x1);
    Axis.insert(a[i].x2);
    In[a[i].x2].push_back(i);
    Out[a[i].x1].push_back(i);
  }
  vector<pair<long long, long long>> Temp(n);
  for (int i = 0; i < n; i++) Temp[i] = pair<long long, long long>(0, a[i].y2);
  myNonLazyTree.Build(Temp);
  for (auto it = Axis.rbegin(); it != Axis.rend(); it++) {
    int x = *it;
    for (int i : In[x])
      Version[x] =
          myNonLazyTree.Update(i, pair<long long, long long>(a[i].a, a[i].b));
    for (int i : Out[x])
      Version[x] =
          myNonLazyTree.Update(i, pair<long long, long long>(0, a[i].y1));
  }
  Axis.insert(1e9 + 1);
  Version[1e9 + 1] = 0;
  int q;
  cin >> q;
  while (q--) {
    static long long Last = 0;
    int L, R, x;
    scanf("%d%d%d", &L, &R, &x);
    L--, R--;
    x = ((long long)x + Last) % 1000000000;
    int y = *Axis.lower_bound(x);
    pair<long long, long long> Cur = myNonLazyTree.Query(Version[y], L, R);
    long long Ans = x * Cur.first + Cur.second;
    printf("%I64d\n", Ans);
    Last = Ans;
  }
}

### 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;
pair<long long, long long> operator+(const pair<long long, long long>& p1,
                                     const pair<long long, long long>& p2) {
  return pair<long long, long long>(p1.first + p2.first, p1.second + p2.second);
}
struct PersistentNonLazySegmentTree {
 private:
  const pair<long long, long long> E = pair<long long, long long>(0, 0);

 private:
  static const int Nax = 100 * 100100;

 private:
  int n;

 private:
  int Counter;

 private:
  int Left[Nax];

 private:
  int Right[Nax];

 private:
  pair<long long, long long> Tree[Nax];

 private:
  vector<int> Roots;

 private:
  int NewNode() {
    Tree[Counter] = E;
    Left[Counter] = -1;
    Right[Counter] = -1;
    return Counter++;
  }

 private:
  int NewNode(int Copy) {
    Tree[Counter] = Tree[Copy];
    Left[Counter] = Left[Copy];
    Right[Counter] = Right[Copy];
    return Counter++;
  }

 private:
  pair<long long, long long> Unite(pair<long long, long long> x,
                                   pair<long long, long long> y) {
    return x + y;
  }

 private:
  void Merge(int Node) {
    Tree[Node] = Unite(Tree[Left[Node]], Tree[Right[Node]]);
  }

 public:
  int Build(const vector<pair<long long, long long>>& a) {
    Clear();
    n = a.size();
    Roots.push_back(NewNode());
    Build(a, Roots[0], 0, n - 1);
    return (int)Roots.size() - 1;
  }

 private:
  void Build(const vector<pair<long long, long long>>& a, int Node, int L,
             int R) {
    if (L == R) return void(Tree[Node] = a[L]);
    Build(a, Left[Node] = NewNode(), L, ((L + R) >> 1));
    Build(a, Right[Node] = NewNode(), ((L + R) >> 1) + 1, R);
    Merge(Node);
  }

 public:
  int Update(int i, pair<long long, long long> x) {
    Roots.push_back(Update(i, x, Roots.back(), 0, n - 1));
    return (int)Roots.size() - 1;
  }

 private:
  int Update(int i, pair<long long, long long> x, int Node, int L, int R) {
    if (i < L || R < i) return Node;
    Node = NewNode(Node);
    if (i <= L && R <= i) return Tree[Node] = x, Node;
    if (i <= ((L + R) >> 1))
      Left[Node] = Update(i, x, Left[Node], L, ((L + R) >> 1));
    else
      Right[Node] = Update(i, x, Right[Node], ((L + R) >> 1) + 1, R);
    return Merge(Node), Node;
  }

 public:
  pair<long long, long long> Query(int Version, int L, int R) {
    return Query(L, R, Roots[Version], 0, n - 1);
  }

 private:
  pair<long long, long long> Query(int i, int j, int Node, int L, int R) {
    if (i > j || L > R) return E;
    if (j < L || R < i) return E;
    if (i <= L && R <= j) return Tree[Node];
    return Unite(Query(i, j, Left[Node], L, ((L + R) >> 1)),
                 Query(i, j, Right[Node], ((L + R) >> 1) + 1, R));
  }

 public:
  void Clear() {
    n = 0;
    Counter = 0;
    Roots.clear();
  }
} myNonLazyTree;
struct Function {
  int a, b;
  int x1, y1;
  int x2, y2;
  Function(long long a = 0, long long b = 0, long long x1 = 0, long long y1 = 0,
           long long x2 = 0, long long y2 = 0)
      : a(a), b(b), x1(x1), y1(y1), x2(x2), y2(y2) {}
  void Read() { scanf("%d%d%d%d%d%d", &x1, &x2, &y1, &a, &b, &y2); }
};
int main() {
  int n;
  cin >> n;
  vector<Function> a(n);
  for (int i = 0; i < n; i++) a[i].Read();
  set<int> Axis;
  map<int, int> Version;
  map<int, vector<int>> In;
  map<int, vector<int>> Out;
  for (int i = 0; i < n; i++) {
    Axis.insert(a[i].x1);
    Axis.insert(a[i].x2);
    In[a[i].x2].push_back(i);
    Out[a[i].x1].push_back(i);
  }
  vector<pair<long long, long long>> Temp(n);
  for (int i = 0; i < n; i++) Temp[i] = pair<long long, long long>(0, a[i].y2);
  myNonLazyTree.Build(Temp);
  for (auto it = Axis.rbegin(); it != Axis.rend(); it++) {
    int x = *it;
    for (int i : In[x])
      Version[x] =
          myNonLazyTree.Update(i, pair<long long, long long>(a[i].a, a[i].b));
    for (int i : Out[x])
      Version[x] =
          myNonLazyTree.Update(i, pair<long long, long long>(0, a[i].y1));
  }
  Axis.insert(1e9 + 1);
  Version[1e9 + 1] = 0;
  int q;
  cin >> q;
  while (q--) {
    static long long Last = 0;
    int L, R, x;
    scanf("%d%d%d", &L, &R, &x);
    L--, R--;
    x = ((long long)x + Last) % 1000000000;
    int y = *Axis.lower_bound(x);
    pair<long long, long long> Cur = myNonLazyTree.Query(Version[y], L, R);
    long long Ans = x * Cur.first + Cur.second;
    printf("%I64d\n", Ans);
    Last = Ans;
  }
}

```