#include <bits/stdc++.h>
using namespace std;
int IN() {
  int x = 0;
  char ch;
  for ((ch = getchar()); ch > '9' || ch < '0'; (ch = getchar()))
    ;
  for (; ch >= '0' && ch <= '9'; (ch = getchar()))
    x = (x << 1) + (x << 3) + ch - '0';
  return x;
}
int n, X1, X2, Y1, Y2, a, b;
int l, r, x, m;
long long ans, Right[200005];
struct Nod {
  vector<pair<int, long long> > Add, Mul;
} Tree[262144 << 1];
void Modify(int x, int y, int z, int w) {
  for (x += 262144, y += 262144; x <= y; x >>= 1, y >>= 1) {
    if (x & 1) {
      long long tmp = z;
      int Size = Tree[x].Add.size();
      if (Size) tmp += Tree[x].Add[Size - 1].second;
      Tree[x++].Add.push_back(make_pair(w, tmp));
    }
    if (!(y & 1)) {
      long long tmp = z;
      int Size = Tree[y].Add.size();
      if (Size) tmp += Tree[y].Add[Size - 1].second;
      Tree[y--].Add.push_back(make_pair(w, tmp));
    }
  }
}
void UP(int x, int y, int z, int w) {
  for (x += 262144, y += 262144; x <= y; x >>= 1, y >>= 1) {
    if (x & 1) {
      long long tmp = z;
      int Size = Tree[x].Mul.size();
      if (Size) tmp += Tree[x].Mul[Size - 1].second;
      Tree[x++].Mul.push_back(make_pair(w, tmp));
    }
    if (!(y & 1)) {
      long long tmp = z;
      int Size = Tree[y].Mul.size();
      if (Size) tmp += Tree[y].Mul[Size - 1].second;
      Tree[y--].Mul.push_back(make_pair(w, tmp));
    }
  }
}
int DIV_Add(int l, int r, int z, int w) {
  for (; l <= r;) {
    int mid = l + r >> 1;
    if (Tree[w].Add[mid].first > z)
      r = mid - 1;
    else
      l = mid + 1;
  }
  return l;
}
int DIV_Mul(int l, int r, int z, int w) {
  for (; l <= r;) {
    int mid = l + r >> 1;
    if (Tree[w].Mul[mid].first > z)
      r = mid - 1;
    else
      l = mid + 1;
  }
  return l;
}
long long Ask(int x, int y, int z) {
  long long tmp = 0;
  int p = z;
  for (z += 262144; z; z >>= 1) {
    int Size_Add = Tree[z].Add.size();
    int Size_Mul = Tree[z].Mul.size();
    if (Size_Add) {
      int R = DIV_Add(0, Size_Add - 1, y, z);
      int L = DIV_Add(0, Size_Add - 1, x - 1, z);
      if (R <= Size_Add && R) tmp += Tree[z].Add[R - 1].second;
      if (L <= Size_Add && L) tmp -= Tree[z].Add[L - 1].second;
    }
    if (Size_Mul) {
      int R = DIV_Mul(0, Size_Mul - 1, y, z);
      int L = DIV_Mul(0, Size_Mul - 1, x - 1, z);
      if (R <= Size_Mul && R) tmp += Tree[z].Mul[R - 1].second * p;
      if (L <= Size_Add && L) tmp -= Tree[z].Mul[L - 1].second * p;
    }
  }
  return tmp;
}
int main() {
  n = IN();
  for (int i = 1; i <= n; i++) {
    X1 = IN();
    X2 = IN();
    Y1 = IN();
    a = IN();
    b = IN();
    Y2 = IN();
    Modify(0, X1, Y1, i);
    Modify(X2 + 1, 262144 - 1, Y2, i);
    Right[i] = Right[i - 1] + Y2;
    Modify(X1 + 1, X2, b, i);
    UP(X1 + 1, X2, a, i);
  }
  m = IN();
  while (m--) {
    l = IN();
    r = IN();
    x = IN();
    x = (ans + x) % 1000000000;
    if (x >= 262144)
      ans = Right[r] - Right[l - 1];
    else
      ans = Ask(l, r, x);
    printf("%lld\n", ans);
  }
}

### Prompt

Please formulate 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;
int IN() {
  int x = 0;
  char ch;
  for ((ch = getchar()); ch > '9' || ch < '0'; (ch = getchar()))
    ;
  for (; ch >= '0' && ch <= '9'; (ch = getchar()))
    x = (x << 1) + (x << 3) + ch - '0';
  return x;
}
int n, X1, X2, Y1, Y2, a, b;
int l, r, x, m;
long long ans, Right[200005];
struct Nod {
  vector<pair<int, long long> > Add, Mul;
} Tree[262144 << 1];
void Modify(int x, int y, int z, int w) {
  for (x += 262144, y += 262144; x <= y; x >>= 1, y >>= 1) {
    if (x & 1) {
      long long tmp = z;
      int Size = Tree[x].Add.size();
      if (Size) tmp += Tree[x].Add[Size - 1].second;
      Tree[x++].Add.push_back(make_pair(w, tmp));
    }
    if (!(y & 1)) {
      long long tmp = z;
      int Size = Tree[y].Add.size();
      if (Size) tmp += Tree[y].Add[Size - 1].second;
      Tree[y--].Add.push_back(make_pair(w, tmp));
    }
  }
}
void UP(int x, int y, int z, int w) {
  for (x += 262144, y += 262144; x <= y; x >>= 1, y >>= 1) {
    if (x & 1) {
      long long tmp = z;
      int Size = Tree[x].Mul.size();
      if (Size) tmp += Tree[x].Mul[Size - 1].second;
      Tree[x++].Mul.push_back(make_pair(w, tmp));
    }
    if (!(y & 1)) {
      long long tmp = z;
      int Size = Tree[y].Mul.size();
      if (Size) tmp += Tree[y].Mul[Size - 1].second;
      Tree[y--].Mul.push_back(make_pair(w, tmp));
    }
  }
}
int DIV_Add(int l, int r, int z, int w) {
  for (; l <= r;) {
    int mid = l + r >> 1;
    if (Tree[w].Add[mid].first > z)
      r = mid - 1;
    else
      l = mid + 1;
  }
  return l;
}
int DIV_Mul(int l, int r, int z, int w) {
  for (; l <= r;) {
    int mid = l + r >> 1;
    if (Tree[w].Mul[mid].first > z)
      r = mid - 1;
    else
      l = mid + 1;
  }
  return l;
}
long long Ask(int x, int y, int z) {
  long long tmp = 0;
  int p = z;
  for (z += 262144; z; z >>= 1) {
    int Size_Add = Tree[z].Add.size();
    int Size_Mul = Tree[z].Mul.size();
    if (Size_Add) {
      int R = DIV_Add(0, Size_Add - 1, y, z);
      int L = DIV_Add(0, Size_Add - 1, x - 1, z);
      if (R <= Size_Add && R) tmp += Tree[z].Add[R - 1].second;
      if (L <= Size_Add && L) tmp -= Tree[z].Add[L - 1].second;
    }
    if (Size_Mul) {
      int R = DIV_Mul(0, Size_Mul - 1, y, z);
      int L = DIV_Mul(0, Size_Mul - 1, x - 1, z);
      if (R <= Size_Mul && R) tmp += Tree[z].Mul[R - 1].second * p;
      if (L <= Size_Add && L) tmp -= Tree[z].Mul[L - 1].second * p;
    }
  }
  return tmp;
}
int main() {
  n = IN();
  for (int i = 1; i <= n; i++) {
    X1 = IN();
    X2 = IN();
    Y1 = IN();
    a = IN();
    b = IN();
    Y2 = IN();
    Modify(0, X1, Y1, i);
    Modify(X2 + 1, 262144 - 1, Y2, i);
    Right[i] = Right[i - 1] + Y2;
    Modify(X1 + 1, X2, b, i);
    UP(X1 + 1, X2, a, i);
  }
  m = IN();
  while (m--) {
    l = IN();
    r = IN();
    x = IN();
    x = (ans + x) % 1000000000;
    if (x >= 262144)
      ans = Right[r] - Right[l - 1];
    else
      ans = Ask(l, r, x);
    printf("%lld\n", ans);
  }
}

```