#include <bits/stdc++.h>
using namespace std;
int n, m, md = 1e9;
struct F {
  int x1, x2, y1, a, b, y2;
  F() {}
} fn[75000];
vector<pair<int, pair<int, long long> > > s[300000];
vector<pair<int, int> > fs[300000];
inline int gtp(int id, int x, bool p) {
  if (p) {
    if (x == -1) return 0;
    if (x == fn[id].x1) return fn[id].a;
    return -fn[id].a;
  }
  if (x == -1) return fn[id].y1;
  if (x == fn[id].x1) return fn[id].b - fn[id].y1;
  return fn[id].y2 - fn[id].b;
}
void bld(int l = 0, int r = n, int id = 1) {
  if (l + 1 == r) {
    s[id].push_back(make_pair(0, make_pair(0, fn[l].y1)));
    s[id].push_back(make_pair(fn[l].x1 + 1, make_pair(fn[l].a, fn[l].b)));
    s[id].push_back(make_pair(fn[l].x2 + 1, make_pair(0, fn[l].y2)));
    fs[id].push_back(make_pair(0, l));
    fs[id].push_back(make_pair(fn[l].x1 + 1, l));
    fs[id].push_back(make_pair(fn[l].x2 + 1, l));
    return;
  }
  int m = l + r >> 1, a = id << 1, b = a | 1;
  bld(l, m, a);
  bld(m, r, b);
  int p = 0;
  long long q = 0ll;
  int i = 0, j = 0;
  while (i < fs[a].size() || j < fs[b].size()) {
    int z = 0;
    if (i == fs[a].size()) {
      if (j + 1 < fs[b].size() && fs[b][j].first == fs[b][j + 1].first)
        z = 4;
      else
        z = 12;
    } else if (j == fs[b].size()) {
      if (i + 1 < fs[a].size() && fs[a][i].first == fs[a][i + 1].first)
        z = 1;
      else
        z = 3;
    } else if (fs[a][i].first < fs[b][j].first) {
      if (i + 1 < fs[a].size() && fs[a][i].first == fs[a][i + 1].first)
        z = 1;
      else
        z = 3;
    } else if (fs[a][i].first > fs[b][j].first) {
      if (j + 1 < fs[b].size() && fs[b][j].first == fs[b][j + 1].first)
        z = 4;
      else
        z = 12;
    } else
      z = 1;
    if (z & 1)
      p += gtp(fs[a][i].second, fs[a][i].first - 1, true),
          q += gtp(fs[a][i].second, fs[a][i].first - 1, false);
    if (z & 4)
      p += gtp(fs[b][j].second, fs[b][j].first - 1, true),
          q += gtp(fs[b][j].second, fs[b][j].first - 1, false);
    if (z & 2) s[id].push_back(make_pair(fs[a][i].first, make_pair(p, q)));
    if (z & 8) s[id].push_back(make_pair(fs[b][j].first, make_pair(p, q)));
    if (z & 1)
      fs[id].push_back(fs[a][i++]);
    else
      fs[id].push_back(fs[b][j++]);
  }
}
int X;
long long gt(int x, int y, int l = 0, int r = n, int id = 1) {
  if (l >= y || r <= x) return 0;
  if (l >= x && r <= y) {
    vector<pair<int, pair<int, long long> > >::iterator it = --lower_bound(
        s[id].begin(), s[id].end(), make_pair(X + 1, make_pair(0, 0ll)));
    return (long long)X * it->second.first + it->second.second;
  }
  int m = l + r >> 1, a = id << 1, b = a | 1;
  return gt(x, y, l, m, a) + gt(x, y, m, r, b);
}
int main() {
  scanf("%d", &n);
  for (int i = 0; i < (int)(n); ++i)
    scanf("%d%d%d%d%d%d", &fn[i].x1, &fn[i].x2, &fn[i].y1, &fn[i].a, &fn[i].b,
          &fn[i].y2);
  bld();
  long long last = 0;
  scanf("%d", &m);
  while (m--) {
    int l, r, x;
    scanf("%d%d%d", &l, &r, &x);
    --l, --r;
    X = (last + x) % md;
    last = gt(l, r + 1);
    printf("%lld\n", last);
  }
}

### 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;
int n, m, md = 1e9;
struct F {
  int x1, x2, y1, a, b, y2;
  F() {}
} fn[75000];
vector<pair<int, pair<int, long long> > > s[300000];
vector<pair<int, int> > fs[300000];
inline int gtp(int id, int x, bool p) {
  if (p) {
    if (x == -1) return 0;
    if (x == fn[id].x1) return fn[id].a;
    return -fn[id].a;
  }
  if (x == -1) return fn[id].y1;
  if (x == fn[id].x1) return fn[id].b - fn[id].y1;
  return fn[id].y2 - fn[id].b;
}
void bld(int l = 0, int r = n, int id = 1) {
  if (l + 1 == r) {
    s[id].push_back(make_pair(0, make_pair(0, fn[l].y1)));
    s[id].push_back(make_pair(fn[l].x1 + 1, make_pair(fn[l].a, fn[l].b)));
    s[id].push_back(make_pair(fn[l].x2 + 1, make_pair(0, fn[l].y2)));
    fs[id].push_back(make_pair(0, l));
    fs[id].push_back(make_pair(fn[l].x1 + 1, l));
    fs[id].push_back(make_pair(fn[l].x2 + 1, l));
    return;
  }
  int m = l + r >> 1, a = id << 1, b = a | 1;
  bld(l, m, a);
  bld(m, r, b);
  int p = 0;
  long long q = 0ll;
  int i = 0, j = 0;
  while (i < fs[a].size() || j < fs[b].size()) {
    int z = 0;
    if (i == fs[a].size()) {
      if (j + 1 < fs[b].size() && fs[b][j].first == fs[b][j + 1].first)
        z = 4;
      else
        z = 12;
    } else if (j == fs[b].size()) {
      if (i + 1 < fs[a].size() && fs[a][i].first == fs[a][i + 1].first)
        z = 1;
      else
        z = 3;
    } else if (fs[a][i].first < fs[b][j].first) {
      if (i + 1 < fs[a].size() && fs[a][i].first == fs[a][i + 1].first)
        z = 1;
      else
        z = 3;
    } else if (fs[a][i].first > fs[b][j].first) {
      if (j + 1 < fs[b].size() && fs[b][j].first == fs[b][j + 1].first)
        z = 4;
      else
        z = 12;
    } else
      z = 1;
    if (z & 1)
      p += gtp(fs[a][i].second, fs[a][i].first - 1, true),
          q += gtp(fs[a][i].second, fs[a][i].first - 1, false);
    if (z & 4)
      p += gtp(fs[b][j].second, fs[b][j].first - 1, true),
          q += gtp(fs[b][j].second, fs[b][j].first - 1, false);
    if (z & 2) s[id].push_back(make_pair(fs[a][i].first, make_pair(p, q)));
    if (z & 8) s[id].push_back(make_pair(fs[b][j].first, make_pair(p, q)));
    if (z & 1)
      fs[id].push_back(fs[a][i++]);
    else
      fs[id].push_back(fs[b][j++]);
  }
}
int X;
long long gt(int x, int y, int l = 0, int r = n, int id = 1) {
  if (l >= y || r <= x) return 0;
  if (l >= x && r <= y) {
    vector<pair<int, pair<int, long long> > >::iterator it = --lower_bound(
        s[id].begin(), s[id].end(), make_pair(X + 1, make_pair(0, 0ll)));
    return (long long)X * it->second.first + it->second.second;
  }
  int m = l + r >> 1, a = id << 1, b = a | 1;
  return gt(x, y, l, m, a) + gt(x, y, m, r, b);
}
int main() {
  scanf("%d", &n);
  for (int i = 0; i < (int)(n); ++i)
    scanf("%d%d%d%d%d%d", &fn[i].x1, &fn[i].x2, &fn[i].y1, &fn[i].a, &fn[i].b,
          &fn[i].y2);
  bld();
  long long last = 0;
  scanf("%d", &m);
  while (m--) {
    int l, r, x;
    scanf("%d%d%d", &l, &r, &x);
    --l, --r;
    X = (last + x) % md;
    last = gt(l, r + 1);
    printf("%lld\n", last);
  }
}

```