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>
using namespace std;
int N, M, T[100005 * 4], B[20][100005];
long long Ans, F[20][100005], G[20][100005];
struct Nod {
  int s, m, r, d;
} A[100005];
bool Cmp(int a, int b) { return A[a].d < A[b].d; }
void Build(int u, int l, int r, int d) {
  if (l == r) {
    B[d][l] = l;
    F[d][l] = A[l].m;
    G[d][l] = A[l].r;
    return;
  }
  int Mid = l + r >> 1;
  Build(u << 1, l, Mid, d + 1);
  Build(u << 1 | 1, Mid + 1, r, d + 1);
  merge(B[d + 1] + l, B[d + 1] + Mid + 1, B[d + 1] + Mid + 1, B[d + 1] + r + 1,
        B[d] + l, Cmp);
  for (int i = l; i <= r; i++)
    F[d][i] = (i == l ? 0 : F[d][i - 1]) + A[B[d][i]].m;
  for (int i = r; i >= l; i--)
    G[d][i] = (i == r ? 0 : G[d][i + 1]) + A[B[d][i]].r;
}
void Dn(int u) {
  if (T[u] != -1) T[u << 1] = T[u], T[u << 1 | 1] = T[u];
}
void Up(int u) { T[u] = (T[u << 1] == T[u << 1 | 1] ? T[u << 1] : -1); }
void Query(int u, int l, int r, int t, int x, int y, int d) {
  if (x <= l && r <= y && T[u] != -1) {
    if (!T[u]) {
      for (int i = l; i <= r; i++)
        Ans += min(A[i].m + 0ll, 1ll * t * A[i].r + A[i].s);
    } else {
      t -= T[u];
      A[N + 1].d = t;
      int x = upper_bound(B[d] + l, B[d] + r + 1, N + 1, Cmp) - B[d] - 1;
      Ans += (x >= l ? F[d][x] : 0) + 1ll * (x + 1 <= r ? G[d][x + 1] : 0) * t;
    }
    T[u] += t;
    return;
  }
  int Mid = l + r >> 1;
  Dn(u);
  if (x <= Mid) Query(u << 1, l, Mid, t, x, y, d + 1);
  if (y > Mid) Query(u << 1 | 1, Mid + 1, r, t, x, y, d + 1);
  Up(u);
}
int main() {
  scanf("%d", &N);
  for (int i = 1; i <= N; i++)
    scanf("%d%d%d", &A[i].s, &A[i].m, &A[i].r),
        A[i].d = (!A[i].r ? 1 << 30 : A[i].m / A[i].r + 1);
  Build(1, 1, N, 0);
  scanf("%d", &M);
  for (int t, l, r; M--;) {
    scanf("%d%d%d", &t, &l, &r);
    Ans = 0;
    Query(1, 1, N, t, l, r, 0);
    printf("%I64d\n", Ans);
  }
}