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 ww {
  long long s, m, r;
} a[100010], Q[100010];
int i, j, k, n, m;
int id[100010], ti[100010], cov[100010 * 4];
long long an[100010];
inline bool cc2(const int &A, const int &B) { return ti[A] < ti[B]; }
struct X {
  vector<ww> tr[100010 * 4];
  inline void zuo(int q, int x, int y) {
    int i;
    long long s = 0, zhi = 0;
    for (i = x; i <= y; i++) {
      if (!a[i].r)
        ti[i] = 1000000000;
      else
        ti[i] = (a[i].m - a[i].s) / a[i].r + ((a[i].m - a[i].s) % a[i].r > 0);
      id[i] = i;
      s += a[i].r;
      zhi += a[i].s;
    }
    sort(id + x, id + y + 1, cc2);
    int last = 0;
    for (i = x; i <= y; i++) {
      int A = id[i];
      if (ti[A] > last) tr[q].push_back((ww){zhi, last, s});
      last = ti[A];
      s -= a[A].r;
      zhi = zhi - a[A].s + a[A].m;
    }
    tr[q].push_back((ww){zhi, last, s});
  }
  void build(int q, int x, int y) {
    zuo(q, x, y);
    if (x == y) return;
    int mid = (x + y) / 2;
    build(q * 2, x, mid);
    build(q * 2 + 1, mid + 1, y);
  }
  inline void jin(int x, int ti, int id) {
    ti = Q[id].s - ti;
    int l = 0, r = tr[x].size() - 1, mid;
    for (; l <= r;) {
      mid = (l + r) / 2;
      if (tr[x][mid].m <= ti)
        l = mid + 1;
      else
        r = mid - 1;
    }
    l--;
    an[id] += tr[x][l].s + tr[x][l].r * ti;
  }
} b[2];
inline bool cc1(const int &A, const int &B) { return Q[A].s < Q[B].s; }
inline void inc(int x, int y) { cov[x] = y; }
inline void renew(int x) {
  if (cov[x] >= 0) inc(x * 2, cov[x]), inc(x * 2 + 1, cov[x]);
  cov[x] = -1;
}
void clear(int q, int x, int y, int id) {
  if (cov[q] >= 0) {
    b[cov[q] > 0].jin(q, cov[q], id);
    return;
  }
  int mid = (x + y) / 2;
  clear(q * 2, x, mid, id);
  clear(q * 2 + 1, mid + 1, y, id);
}
void cover(int q, int x, int y, int l, int r, int id) {
  if (l <= x && y <= r) {
    clear(q, x, y, id);
    inc(q, Q[id].s);
    return;
  }
  renew(q);
  int mid = (x + y) / 2;
  if (l <= mid) cover(q * 2, x, mid, l, r, id);
  if (mid < r) cover(q * 2 + 1, mid + 1, y, l, r, id);
}
int main() {
  scanf("%d", &n);
  for (i = 1; i <= n; i++) scanf("%I64d%I64d%I64d", &a[i].s, &a[i].m, &a[i].r);
  b[0].build(1, 1, n);
  for (i = 1; i <= n; i++) a[i].s = 0;
  b[1].build(1, 1, n);
  scanf("%d", &m);
  for (i = 1; i <= m; i++) {
    int x, y, z;
    scanf("%d%d%d", &x, &y, &z);
    Q[i] = (ww){x, y, z};
    id[i] = i;
  }
  sort(id + 1, id + m + 1, cc1);
  for (i = 1; i <= 100010 * 4 - 1; i++) cov[i] = -1;
  cov[1] = 0;
  for (i = 1; i <= m; i++) {
    int A = id[i];
    cover(1, 1, n, Q[A].m, Q[A].r, A);
  }
  for (i = 1; i <= m; i++) printf("%I64d\n", an[i]);
  return 0;
}