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 maxn = 100010;
const int inf = 2e9;
int s[maxn], m[maxn], r[maxn], t[maxn], vis[maxn << 2];
int T[20][maxn];
long long pre[20][maxn], v[20][maxn];
struct in {
  int m, r, t;
  in() {}
  in(int mm, int rr, int tt) : m(mm), r(rr), t(tt) {}
  bool friend operator<(in w, in v) { return w.t < v.t; }
} fcy[maxn];
void build(int dep, int Now, int L, int R) {
  for (int i = L; i <= R; i++) fcy[i] = in(m[i], r[i], t[i]);
  sort(fcy + L, fcy + R + 1);
  for (int i = L; i <= R; i++) {
    T[dep][i] = fcy[i].t;
    pre[dep][i] = fcy[i].m;
    v[dep][i] = fcy[i].r;
    if (i > L) {
      pre[dep][i] += pre[dep][i - 1];
      v[dep][i] += v[dep][i - 1];
    }
  }
  if (L == R) {
    vis[Now] = 0;
    return;
  }
  vis[Now] = -1;
  int Mid = (L + R) >> 1;
  build(dep + 1, Now << 1, L, Mid);
  build(dep + 1, Now << 1 | 1, Mid + 1, R);
}
long long query(int dep, int Now, int L, int R, int ql, int qr, int times) {
  if (vis[Now] == 0) {
    vis[Now] = times;
    if (s[L] + 1LL * times * r[L] >= m[L]) return 1LL * m[L];
    return 1LL * s[L] + times * r[L];
  }
  if (ql <= L && qr >= R && vis[Now] > 0) {
    int tp = times - vis[Now];
    int p = upper_bound(T[dep] + L, T[dep] + R + 1, tp) - T[dep];
    p--;
    long long res = 0;
    if (p >= L) res = pre[dep][p];
    long long vd = v[dep][R];
    if (p >= L) vd -= v[dep][p];
    res += vd * tp;
    vis[Now] = times;
    return res;
  }
  if (vis[Now] >= 0) vis[Now << 1] = vis[Now << 1 | 1] = vis[Now];
  int Mid = (L + R) >> 1;
  long long res = 0;
  if (ql <= Mid) res += query(dep + 1, Now << 1, L, Mid, ql, qr, times);
  if (qr > Mid) res += query(dep + 1, Now << 1 | 1, Mid + 1, R, ql, qr, times);
  if (vis[Now << 1] == vis[Now << 1 | 1])
    vis[Now] = vis[Now << 1];
  else
    vis[Now] = -1;
  return res;
}
int main() {
  int N, M, ts, L, R;
  scanf("%d", &N);
  for (int i = 1; i <= N; i++) {
    scanf("%d%d%d", &s[i], &m[i], &r[i]);
    if (!r[i])
      t[i] = inf;
    else
      t[i] = m[i] / r[i] + (m[i] % r[i] ? 1 : 0);
  }
  build(1, 1, 1, N);
  scanf("%d", &M);
  for (int i = 1; i <= M; i++) {
    scanf("%d%d%d", &ts, &L, &R);
    long long ans = query(1, 1, 1, N, L, R, ts);
    printf("%lld\n", ans);
  }
  return 0;
}