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;
int n, sl, fh;
struct node {
  int s, m, r, t;
} a[100010];
struct seg {
  vector<long long> tim, pre, suf;
  vector<node> a;
  int lt;
  void resize(int l) {
    pre.resize(l);
    suf.resize(l);
  }
  long long getans(int T) {
    if (!lt) {
      long long ret = 0;
      for (int i = 0; i < (int)a.size(); ++i)
        ret += min(0ll + a[i].m, a[i].s + 1ll * T * a[i].r);
      lt = T;
      return ret;
    }
    int t = T - lt,
        p = upper_bound(tim.begin(), tim.end(), t) - tim.begin() - 1;
    long long ret = 0;
    if (p >= 0) ret = pre[p];
    if (p < (int)a.size() - 1) ret += t * suf[p + 1];
    lt = T;
    return ret;
  }
} tr[100010 << 2];
int rd() {
  sl = 0;
  fh = 1;
  char ch = getchar();
  while (ch < '0' || '9' < ch) {
    if (ch == '-') fh = -1;
    ch = getchar();
  }
  while ('0' <= ch && ch <= '9') sl = sl * 10 + ch - '0', ch = getchar();
  return sl * fh;
}
void merge(seg &res, seg &x, seg &y) {
  int sz, sx = x.a.size(), sy = y.a.size();
  res.resize(sz = sx + sy);
  int i = 0, j = 0;
  for (; i < sx && j < sy;)
    if (x.tim[i] <= y.tim[j])
      res.a.push_back(x.a[i]), res.tim.push_back(x.tim[i]), ++i;
    else
      res.a.push_back(y.a[j]), res.tim.push_back(y.tim[j]), ++j;
  for (; i < sx; ++i) res.a.push_back(x.a[i]), res.tim.push_back(x.tim[i]);
  for (; j < sy; ++j) res.a.push_back(y.a[j]), res.tim.push_back(y.tim[j]);
  res.pre[0] = res.a[0].m;
  for (int i = 1; i < sz; ++i) res.pre[i] = res.pre[i - 1] + res.a[i].m;
  res.suf[sz - 1] = res.a[sz - 1].r;
  for (int i = sz - 1; i; --i) res.suf[i - 1] = res.suf[i] + res.a[i - 1].r;
}
void build(int x, int l, int r) {
  if (l == r) {
    tr[x].a.push_back(a[l]);
    tr[x].tim.push_back(a[l].t);
    tr[x].pre.push_back(a[l].m);
    tr[x].suf.push_back(a[l].r);
    return;
  }
  build(x << 1, l, ((l + r) >> 1));
  build(x << 1 | 1, ((l + r) >> 1) + 1, r);
  merge(tr[x], tr[x << 1], tr[x << 1 | 1]);
}
void pushdown(int x) {
  if (tr[x].lt >= 0) tr[x << 1].lt = tr[x << 1 | 1].lt = tr[x].lt, tr[x].lt = 0;
}
long long query(int x, int l, int r, int L, int R, int T) {
  if (L <= l && r <= R && tr[x].lt >= 0) return tr[x].getans(T);
  pushdown(x);
  tr[x].lt = -1;
  long long ret = 0;
  if (L <= ((l + r) >> 1)) ret = query(x << 1, l, ((l + r) >> 1), L, R, T);
  if (R > ((l + r) >> 1))
    ret += query(x << 1 | 1, ((l + r) >> 1) + 1, r, L, R, T);
  if (tr[x << 1].lt == tr[x << 1 | 1].lt) tr[x].lt = tr[x << 1].lt;
  return ret;
}
int main() {
  n = rd();
  for (int i = 1; i <= n; ++i) {
    a[i].s = rd();
    a[i].m = rd();
    a[i].r = rd();
    if (!a[i].r)
      a[i].t = 1e9;
    else
      a[i].t = (a[i].m - 1) / a[i].r + 1;
  }
  build(1, 1, n);
  for (int l, r, t, T = rd(); T; --T) {
    t = rd();
    l = rd();
    r = rd();
    printf("%lld\n", query(1, 1, n, l, r, t));
  }
  return 0;
}