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;
struct data {
  int s, m, r, t;
  int operator<(const data &b) const { return t < b.t; }
  void get() {
    scanf("%d", &s);
    scanf("%d", &m);
    scanf("%d", &r);
    t = r ? (m / r + (m % r > 0)) : 2e9;
  }
  int calc(int o) { return min(1ll * m, s + 1ll * r * o); }
} a[20][100005];
int T[100005 << 2];
long long R[20][100005], sum[20][100005], ans;
int n, Q, t, ql, qr;
void Build(int x, int l, int r, int d) {
  T[x] = -2;
  if (l == r) {
    a[d][l].get();
    R[d][l] = R[d][l - 1] + a[d][l].r;
    sum[d][l] = sum[d][l - 1] + a[d][l].m;
    return;
  }
  int mid = (l + r) >> 1;
  Build(x << 1, l, mid, d + 1);
  Build(x << 1 | 1, mid + 1, r, d + 1);
  for (int i = l; i <= r; i++) a[d][i] = a[d + 1][i];
  sort(a[d] + l, a[d] + r + 1);
  for (int i = l; i <= r; i++) R[d][i] = R[d][i - 1] + a[d][i].r;
  for (int i = l; i <= r; i++) sum[d][i] = sum[d][i - 1] + a[d][i].m;
}
void Find(int x, int l, int r, int d) {
  if (ql <= l && r <= qr && T[x] != -1) {
    if (l == 9) l = 9;
    if (T[x] == -2) {
      for (int i = l; i <= r; i++) ans += a[d][i].calc(t);
      T[x] = t;
      return;
    }
    data tmp = (data){0, 0, 0, t - T[x]};
    int id = upper_bound(a[d] + l, a[d] + r + 1, tmp) - a[d];
    ans += 1ll * (R[d][r] - R[d][id - 1]) * (t - T[x]) + sum[d][id - 1] -
           sum[d][l - 1];
    T[x] = t;
    return;
  }
  if (T[x] >= 0) T[x << 1] = T[x << 1 | 1] = T[x];
  int mid = (l + r) >> 1;
  if (ql <= mid) Find(x << 1, l, mid, d + 1);
  if (qr > mid) Find(x << 1 | 1, mid + 1, r, d + 1);
  T[x] = (T[x << 1] == T[x << 1 | 1]) ? T[x << 1] : -1;
}
int main() {
  scanf("%d", &n);
  Build(1, 1, n, 0);
  scanf("%d", &Q);
  while (Q--) {
    scanf("%d", &t);
    scanf("%d", &ql);
    scanf("%d", &qr);
    ans = 0;
    Find(1, 1, n, 0);
    printf("%I64d\n", ans);
  }
}