#include <bits/stdc++.h>
using namespace std;
vector<tuple<int, int, int, int, int, int> > F(1);
vector<int> v;
typedef struct node {
  int leftEnd;
  int rightEnd;
  long long a;
  long long b;
  node *leftChild;
  node *rightChild;
} node;
node *CreateNode(int leftEnd, int rightEnd, long long a, long long b) {
  node *temp = new node;
  temp->leftEnd = leftEnd;
  temp->rightEnd = rightEnd;
  temp->a = a;
  temp->b = b;
  temp->leftChild = NULL;
  temp->rightChild = NULL;
  return temp;
}
node *BuildSegmentTree(int i, int j) {
  node *temp = CreateNode(v[i - 1] + 1, v[j], 0ll, 0ll);
  if (i != j) {
    int k = (i + j) / 2;
    temp->leftChild = BuildSegmentTree(i, k);
    temp->rightChild = BuildSegmentTree(k + 1, j);
  }
  return temp;
}
node *PersistentTreeInsert(node *currentNode, int l, int r, int a, int b) {
  if (currentNode == NULL || l > currentNode->rightEnd ||
      r < currentNode->leftEnd)
    return currentNode;
  node *newNode = new node;
  newNode->leftEnd = currentNode->leftEnd;
  newNode->rightEnd = currentNode->rightEnd;
  newNode->a = currentNode->a;
  newNode->b = currentNode->b;
  l = max(l, currentNode->leftEnd);
  r = min(r, currentNode->rightEnd);
  if (l == currentNode->leftEnd && r == currentNode->rightEnd)
    newNode->a += a, newNode->b += b,
        newNode->leftChild = currentNode->leftChild,
        newNode->rightChild = currentNode->rightChild;
  else {
    newNode->leftChild =
        PersistentTreeInsert(currentNode->leftChild, l, r, a, b);
    newNode->rightChild =
        PersistentTreeInsert(currentNode->rightChild, l, r, a, b);
  }
  return newNode;
}
void TraverseSegmentTree(node *root) {
  if (root) {
    TraverseSegmentTree(root->leftChild);
    TraverseSegmentTree(root->rightChild);
    printf("%d %d\n", root->leftEnd, root->rightEnd);
  }
}
void PersistentTreeQuery(node *u, node *v, int x, long long &a, long long &b) {
  a += v->a - u->a, b += v->b - u->b;
  if (v->leftChild != NULL && x <= v->leftChild->rightEnd)
    PersistentTreeQuery(u->leftChild, v->leftChild, x, a, b);
  else {
    if (v->rightChild != NULL && x >= v->rightChild->leftEnd)
      PersistentTreeQuery(u->rightChild, v->rightChild, x, a, b);
  }
}
node *root[75001];
void PreProcess() {
  int n = F.size() - 1;
  v.push_back(-1);
  v.push_back(0);
  for (int x = 1; x <= n; ++x) {
    v.push_back(get<0>(F[x]));
    v.push_back(get<1>(F[x]));
  }
  v.push_back(1000000000);
  sort(v.begin(), v.end());
  v.erase(unique(v.begin(), v.end()), v.end());
  root[0] = BuildSegmentTree(1, v.size() - 1);
  for (int i = 1; i <= n; ++i) {
    int x1, x2, y1, a, b, y2;
    tie(x1, x2, y1, a, b, y2) = F[i];
    root[i] = PersistentTreeInsert(root[i - 1], 0, x1, 0, y1);
    root[i] = PersistentTreeInsert(root[i], x1 + 1, x2, a, b);
    root[i] = PersistentTreeInsert(root[i], x2 + 1, 1000000000, 0, y2);
  }
}
long long Query(int l, int r, int x) {
  long long a = 0, b = 0;
  PersistentTreeQuery(root[l - 1], root[r], x, a, b);
  return (a * x + b);
}
int main() {
  int n, m;
  scanf("%d", &n);
  while (n--) {
    int x1, x2, y1, a, b, y2;
    scanf("%d%d%d%d%d%d", &x1, &x2, &y1, &a, &b, &y2);
    F.push_back(make_tuple(x1, x2, y1, a, b, y2));
  }
  PreProcess();
  scanf("%d", &m);
  long long ans = 0;
  while (m--) {
    int l, r, x;
    scanf("%d%d%d", &l, &r, &x);
    x = (ans + x) % 1000000000;
    ans = Query(l, r, x);
    printf("%lld\n", ans);
  }
  return 0;
}

### Prompt

Develop a solution in CPP 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;
vector<tuple<int, int, int, int, int, int> > F(1);
vector<int> v;
typedef struct node {
  int leftEnd;
  int rightEnd;
  long long a;
  long long b;
  node *leftChild;
  node *rightChild;
} node;
node *CreateNode(int leftEnd, int rightEnd, long long a, long long b) {
  node *temp = new node;
  temp->leftEnd = leftEnd;
  temp->rightEnd = rightEnd;
  temp->a = a;
  temp->b = b;
  temp->leftChild = NULL;
  temp->rightChild = NULL;
  return temp;
}
node *BuildSegmentTree(int i, int j) {
  node *temp = CreateNode(v[i - 1] + 1, v[j], 0ll, 0ll);
  if (i != j) {
    int k = (i + j) / 2;
    temp->leftChild = BuildSegmentTree(i, k);
    temp->rightChild = BuildSegmentTree(k + 1, j);
  }
  return temp;
}
node *PersistentTreeInsert(node *currentNode, int l, int r, int a, int b) {
  if (currentNode == NULL || l > currentNode->rightEnd ||
      r < currentNode->leftEnd)
    return currentNode;
  node *newNode = new node;
  newNode->leftEnd = currentNode->leftEnd;
  newNode->rightEnd = currentNode->rightEnd;
  newNode->a = currentNode->a;
  newNode->b = currentNode->b;
  l = max(l, currentNode->leftEnd);
  r = min(r, currentNode->rightEnd);
  if (l == currentNode->leftEnd && r == currentNode->rightEnd)
    newNode->a += a, newNode->b += b,
        newNode->leftChild = currentNode->leftChild,
        newNode->rightChild = currentNode->rightChild;
  else {
    newNode->leftChild =
        PersistentTreeInsert(currentNode->leftChild, l, r, a, b);
    newNode->rightChild =
        PersistentTreeInsert(currentNode->rightChild, l, r, a, b);
  }
  return newNode;
}
void TraverseSegmentTree(node *root) {
  if (root) {
    TraverseSegmentTree(root->leftChild);
    TraverseSegmentTree(root->rightChild);
    printf("%d %d\n", root->leftEnd, root->rightEnd);
  }
}
void PersistentTreeQuery(node *u, node *v, int x, long long &a, long long &b) {
  a += v->a - u->a, b += v->b - u->b;
  if (v->leftChild != NULL && x <= v->leftChild->rightEnd)
    PersistentTreeQuery(u->leftChild, v->leftChild, x, a, b);
  else {
    if (v->rightChild != NULL && x >= v->rightChild->leftEnd)
      PersistentTreeQuery(u->rightChild, v->rightChild, x, a, b);
  }
}
node *root[75001];
void PreProcess() {
  int n = F.size() - 1;
  v.push_back(-1);
  v.push_back(0);
  for (int x = 1; x <= n; ++x) {
    v.push_back(get<0>(F[x]));
    v.push_back(get<1>(F[x]));
  }
  v.push_back(1000000000);
  sort(v.begin(), v.end());
  v.erase(unique(v.begin(), v.end()), v.end());
  root[0] = BuildSegmentTree(1, v.size() - 1);
  for (int i = 1; i <= n; ++i) {
    int x1, x2, y1, a, b, y2;
    tie(x1, x2, y1, a, b, y2) = F[i];
    root[i] = PersistentTreeInsert(root[i - 1], 0, x1, 0, y1);
    root[i] = PersistentTreeInsert(root[i], x1 + 1, x2, a, b);
    root[i] = PersistentTreeInsert(root[i], x2 + 1, 1000000000, 0, y2);
  }
}
long long Query(int l, int r, int x) {
  long long a = 0, b = 0;
  PersistentTreeQuery(root[l - 1], root[r], x, a, b);
  return (a * x + b);
}
int main() {
  int n, m;
  scanf("%d", &n);
  while (n--) {
    int x1, x2, y1, a, b, y2;
    scanf("%d%d%d%d%d%d", &x1, &x2, &y1, &a, &b, &y2);
    F.push_back(make_tuple(x1, x2, y1, a, b, y2));
  }
  PreProcess();
  scanf("%d", &m);
  long long ans = 0;
  while (m--) {
    int l, r, x;
    scanf("%d%d%d", &l, &r, &x);
    x = (ans + x) % 1000000000;
    ans = Query(l, r, x);
    printf("%lld\n", ans);
  }
  return 0;
}

```