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;
const int N = 200005;
const int inf = 1000000000;
struct ST {
  int l, r, t;
} t[N << 2];
struct node {
  int s, m, r, t;
  friend bool operator<(node a, node b) { return a.t < b.t; }
  node() {}
  node(int s, int m, int r, int t) : s(s), m(m), r(r), t(t) {}
} p[20][N];
int n, c, L, R, T;
int ss[N], mm[N], rr[N];
long long sm[20][N], sr[20][N];
void update(int k) {
  if (t[k << 1].t == t[k << 1 | 1].t)
    t[k].t = t[k << 1].t;
  else
    t[k].t = -1;
}
void pushdown(int k) {
  if (t[k].t >= 0) t[k << 1].t = t[k << 1 | 1].t = t[k].t;
}
void build(int k, int l, int r, int d) {
  int i, j, z;
  t[k].t = -2;
  t[k].l = l;
  t[k].r = r;
  if (l == r) {
    if (!rr[l])
      z = inf;
    else
      z = (mm[l] + rr[l] - 1) / rr[l];
    p[d][l] = node(ss[l], mm[l], rr[l], z);
    sm[d][l] = sm[d][l - 1] + 1ll * mm[l];
    sr[d][l] = sr[d][l - 1] + 1ll * rr[l];
    return;
  }
  int mid = (l + r) >> 1;
  build(k << 1, l, mid, d + 1);
  build(k << 1 | 1, mid + 1, r, d + 1);
  z = i = l;
  j = mid + 1;
  while (i <= mid && j <= r) {
    if (p[d + 1][i] < p[d + 1][j])
      p[d][z++] = p[d + 1][i++];
    else
      p[d][z++] = p[d + 1][j++];
  }
  while (i <= mid) p[d][z++] = p[d + 1][i++];
  while (j <= r) p[d][z++] = p[d + 1][j++];
  for (i = l; i <= r; i++) sm[d][i] = sm[d][i - 1] + 1ll * p[d][i].m;
  for (i = l; i <= r; i++) sr[d][i] = sr[d][i - 1] + 1ll * p[d][i].r;
  update(k);
}
int calc(int p, int t) {
  return min(1ll * mm[p], 1ll * t * rr[p] + 1ll * ss[p]);
}
long long query(int k, int L, int R, int c, int d) {
  long long res = 0;
  int i, j;
  if (L <= t[k].l && t[k].r <= R && t[k].t != -1) {
    if (t[k].t == -2) {
      for (i = t[k].l; i <= t[k].r; i++) res += calc(i, c);
    } else {
      node w;
      w.t = c - t[k].t;
      j = upper_bound(p[d] + t[k].l, p[d] + t[k].r + 1, w) - p[d] - 1;
      res = sm[d][j] - sm[d][t[k].l - 1] +
            1ll * (c - t[k].t) * (sr[d][t[k].r] - sr[d][j]);
    }
    t[k].t = c;
    return res;
  }
  pushdown(k);
  int mid = (t[k].l + t[k].r) >> 1;
  if (L <= mid) res += query(k << 1, L, R, c, d + 1);
  if (R > mid) res += query(k << 1 | 1, L, R, c, d + 1);
  update(k);
  return res;
}
int main() {
  scanf("%d", &n);
  for (int i = 1; i <= n; i++) scanf("%d%d%d", &ss[i], &mm[i], &rr[i]);
  build(1, 1, n, 0);
  scanf("%d", &T);
  while (T--) {
    scanf("%d%d%d", &c, &L, &R);
    printf("%I64d\n", query(1, L, R, c, 0));
  }
  return 0;
}