Problem: Lord Tirek is a centaur and the main antagonist in the season four finale episodes in the series "My Little Pony: Friendship Is Magic". In "Twilight's Kingdom" (Part 1), Tirek escapes from Tartarus and drains magic from ponies to grow stronger.

<image>

The core skill of Tirek is called Absorb Mana. It takes all mana from a magic creature and gives them to the caster.

Now to simplify the problem, assume you have n ponies (numbered from 1 to n). Each pony has three attributes:

  * si : amount of mana that the pony has at time 0; 
  * mi : maximum mana that the pony can have; 
  * ri : mana regeneration per unit time. 



Lord Tirek will do m instructions, each of them can be described with three integers: ti, li, ri. The instruction means that at time ti, Tirek will use Absorb Mana on ponies with numbers from li to ri (both borders inclusive). We'll give you all the m instructions in order, count how much mana Tirek absorbs for each instruction.

Input

The first line contains an integer n (1 ≤ n ≤ 105) — the number of ponies. Each of the next n lines contains three integers si, mi, ri (0 ≤ si ≤ mi ≤ 105; 0 ≤ ri ≤ 105), describing a pony. 

The next line contains an integer m (1 ≤ m ≤ 105) — the number of instructions. Each of the next m lines contains three integers ti, li, ri (0 ≤ ti ≤ 109; 1 ≤ li ≤ ri ≤ n), describing an instruction of Lord Tirek. The instructions are given in strictly increasing order of ti (all ti are distinct).

Output

For each instruction, output a single line which contains a single integer, the total mana absorbed in this instruction.

Examples

Input

5
0 10 1
0 12 1
0 20 1
0 12 1
0 10 1
2
5 1 5
19 1 5


Output

25
58

Note

Every pony starts with zero mana. For the first instruction, each pony has 5 mana, so you get 25 mana in total and each pony has 0 mana after the first instruction.

For the second instruction, pony 3 has 14 mana and other ponies have mana equal to their mi.

Solution (in CPP):

Solution:
#include <bits/stdc++.h>
long long gi() {
  long long x = 0, f = 1;
  char ch = getchar();
  while (!isdigit(ch)) f ^= ch == '-', ch = getchar();
  while (isdigit(ch)) x = x * 10 + ch - '0', ch = getchar();
  return f ? x : -x;
}
std::mt19937 rnd(time(NULL));
template <class T>
void cxk(T& a, T b) {
  a = a > b ? a : b;
}
template <class T>
void cnk(T& a, T b) {
  a = a < b ? a : b;
}
int st[100010], mx[100100], sp[100010], tm[100010], s[100010], m;
int MXT[400010], MNT[400010], tag[400010];
std::vector<int> vec[400010];
std::vector<long long> S1[400010], S2[400010];
int cmp(int a, int b) { return tm[a] < tm[b]; }
void build(int x, int l, int r) {
  MXT[x] = MNT[x] = tag[x] = -1;
  if (l == r)
    vec[x].push_back(l);
  else {
    build(x << 1, l, ((l + r) >> 1)), build(x << 1 | 1, ((l + r) >> 1) + 1, r);
    vec[x].resize(r - l + 1);
    merge((vec[x << 1]).begin(), (vec[x << 1]).end(), (vec[x << 1 | 1]).begin(),
          (vec[x << 1 | 1]).end(), vec[x].begin(), cmp);
  }
  S1[x].resize(r - l + 1);
  S2[x].resize(r - l + 1);
  for (int i = 0; i < vec[x].size(); ++i)
    S1[x][i] = mx[vec[x][i]], S2[x][i] = sp[vec[x][i]];
  for (int i = 1; i < vec[x].size(); ++i) S1[x][i] += S1[x][i - 1];
  for (int i = vec[x].size() - 2; ~i; --i) S2[x][i] += S2[x][i + 1];
}
long long ans = 0;
void down(int x) {
  if (tag[x] != -1)
    tag[x << 1] = MXT[x << 1] = MNT[x << 1] = tag[x << 1 | 1] =
        MXT[x << 1 | 1] = MNT[x << 1 | 1] = tag[x],
             tag[x] = -1;
}
void update(int x, int l, int r, int L, int R, int t) {
  if (L <= l && r <= R && (~MNT[x] || l == r) && MXT[x] == MNT[x]) {
    if (MXT[x] == -1) {
      ans += std::min((long long)mx[l], 1ll * sp[l] * t + st[l]);
    } else {
      tm[0] = t - MXT[x];
      int p = std::lower_bound((vec[x]).begin(), (vec[x]).end(), 0, cmp) -
              vec[x].begin();
      if (p < vec[x].size()) ans += 1ll * (t - MXT[x]) * S2[x][p];
      if (p) ans += S1[x][p - 1];
    }
    tag[x] = MXT[x] = MNT[x] = t;
    return;
  }
  down(x);
  if (L <= ((l + r) >> 1)) update(x << 1, l, ((l + r) >> 1), L, R, t);
  if (((l + r) >> 1) < R) update(x << 1 | 1, ((l + r) >> 1) + 1, r, L, R, t);
  MXT[x] = std::max(MXT[x << 1], MXT[x << 1 | 1]);
  MNT[x] = std::min(MNT[x << 1], MNT[x << 1 | 1]);
}
int main() {
  int n = gi();
  for (int i = 1; i <= n; ++i)
    st[i] = gi(), mx[i] = gi(), sp[i] = gi(),
    tm[i] = !sp[i] ? 1e9 : mx[i] / sp[i];
  build(1, 1, n);
  int m = gi(), t, l, r;
  while (m--) {
    t = gi(), l = gi(), r = gi();
    ans = 0;
    update(1, 1, n, l, r, t);
    printf("%lld\n", ans);
  }
  return 0;
}