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 blocksize = 400;
const int inf = 1000000000;
struct fish {
  int time, lim, v;
} a[maxn];
bool cmp(fish a, fish b) { return a.time < b.time; }
FILE *fp;
long long orians[maxn / blocksize + 2][maxn + 10];
long long ans, slim[maxn], sv[maxn];
int cur[maxn], lim[maxn], v[maxn], l[maxn], r[maxn], i, j, n, m, n2,
    belong[maxn], curt[maxn], ori[maxn];
void init() {
  fscanf(fp, "%d", &n);
  for (int i = 1; i <= n; ++i) {
    fscanf(fp, "%d%d%d", &cur[i], &lim[i], &v[i]);
    a[i].time = v[i] == 0 ? inf : lim[i] / v[i] + (lim[i] % v[i] == 0 ? 0 : 1);
    a[i].lim = lim[i];
    a[i].v = v[i];
  }
  memset(orians, 0, sizeof(orians));
  n2 = 0;
  for (int i = 1; i <= n / blocksize; ++i) {
    n2++, l[n2] = (n2 - 1) * blocksize + 1, r[n2] = n2 * blocksize;
    sort(a + l[n2], a + r[n2] + 1, cmp);
    slim[l[n2]] = a[l[n2]].lim;
    for (int j = l[n2] + 1; j <= r[n2]; ++j) slim[j] = slim[j - 1] + a[j].lim;
    sv[r[n2]] = a[r[n2]].v;
    for (int j = r[n2] - 1; j >= l[n2]; --j) sv[j] = sv[j + 1] + a[j].v;
    int tmp = l[n2];
    if (i == 2) {
    }
    for (int j = 0; j <= maxn; ++j) {
      while (tmp <= r[n2] && a[tmp].time <= j) ++tmp;
      orians[n2][j] = (tmp <= r[n2] ? (long long)j * sv[tmp] : 0) +
                      (tmp > l[n2] ? slim[tmp - 1] : 0);
    }
  }
  if (n % blocksize != 0) {
    n2++;
    l[n2] = (n2 - 1) * blocksize + 1;
    r[n2] = n;
    sort(a + l[n2], a + r[n2] + 1, cmp);
    slim[l[n2]] = a[l[n2]].lim;
    for (int j = l[n2] + 1; j <= r[n2]; ++j) slim[j] = slim[j - 1] + a[j].lim;
    sv[r[n2]] = a[r[n2]].v;
    for (int j = r[n2] - 1; j >= l[n2]; --j) sv[j] = sv[j + 1] + a[j].v;
    int tmp = l[n2];
    for (int j = 0; j <= maxn; ++j) {
      while (tmp <= r[n2] && a[tmp].time <= j) ++tmp;
      orians[n2][j] = (tmp <= r[n2] ? (long long)j * sv[tmp] : 0) +
                      (tmp > l[n2] ? slim[tmp - 1] : 0);
    }
  }
  for (int i = 1; i <= n2; ++i)
    for (int j = l[i]; j <= r[i]; ++j) belong[j] = i;
}
void spread(int bid, int lbound, int rbound, int t) {
  for (int i = l[bid]; i <= lbound - 1; ++i) {
    long long tmp = (t - curt[bid]) * (long long)v[i] + cur[i];
    cur[i] = tmp <= lim[i] ? tmp : lim[i];
  }
  for (int i = rbound + 1; i <= r[bid]; ++i) {
    long long tmp = (t - curt[bid]) * (long long)v[i] + cur[i];
    cur[i] = tmp <= lim[i] ? tmp : lim[i];
  }
  for (int i = lbound; i <= rbound; ++i) {
    long long tmp = (t - curt[bid]) * (long long)v[i] + cur[i];
    ans += tmp <= lim[i] ? tmp : lim[i];
    cur[i] = 0;
  }
  ori[bid] = 0;
  curt[bid] = t;
}
void calc(int bid, int t) {
  if (ori[bid]) {
    ans += orians[bid][(t - curt[bid]) > maxn ? maxn : (t - curt[bid])];
    curt[bid] = t;
  } else {
    for (int i = l[bid]; i <= r[bid]; ++i) {
      long long tmp = (t - curt[bid]) * (long long)v[i] + cur[i];
      ans += tmp <= lim[i] ? tmp : lim[i];
      cur[i] = 0;
    }
    ori[bid] = 1;
    curt[bid] = t;
  }
}
void work() {
  memset(ori, 0, sizeof(ori));
  memset(curt, 0, sizeof(curt));
  fscanf(fp, "%d", &m);
  for (int i = 1; i <= m; ++i) {
    int x, y, t;
    fscanf(fp, "%d%d%d", &t, &x, &y);
    int xx = belong[x];
    int yy = belong[y];
    ans = 0;
    if (i == 4) {
    }
    if (xx == yy)
      spread(xx, x, y, t);
    else {
      spread(xx, x, r[xx], t);
      spread(yy, l[yy], y, t);
      for (int j = xx + 1; j <= yy - 1; ++j) calc(j, t);
    }
    printf("%I64d\n", ans);
  }
}
int main() {
  fp = stdin;
  init();
  work();
  return 0;
}