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;
long long read() {
  long long f = 1, x = 0;
  char c = getchar();
  while (c > '9' || c < '0') {
    if (c == '-') f = -1;
    c = getchar();
  }
  while (c >= '0' && c <= '9') {
    x = x * 10 + c - '0';
    c = getchar();
  }
  return f * x;
}
int n, m, a[300032], b[300032], c[300032], s[300032];
struct TREE {
  int ti;
  bool op, cvr, nc;
} tr[300032 * 8];
struct node {
  long long sl, sr, a, b, c;
};
bool cmp(node x, node y) { return x.c < y.c; }
bool operator<(node a, node b) { return a.c < b.c; }
vector<node> mts[300032 * 8];
vector<node>::iterator it;
void pushdown(int x) {
  int l = x << 1, r = x << 1 | 1;
  if (tr[x].cvr) {
    tr[x].cvr = 0;
    tr[l].cvr = tr[r].cvr = 1;
    tr[l].op = tr[r].op = 0;
    tr[l].ti = tr[r].ti = tr[x].ti;
  }
}
void pushup(int x) {
  int l = x << 1, r = x << 1 | 1;
  tr[x].op = tr[l].op | tr[r].op | (tr[l].ti != tr[r].ti);
  tr[x].ti = tr[l].ti;
  tr[x].nc = tr[l].nc | tr[r].nc;
}
void build(int rt, int l, int r) {
  tr[rt].nc = 1;
  for (int i = l; i <= r; ++i)
    mts[rt].push_back((node){0, 0, a[i], b[i], c[i]});
  sort(mts[rt].begin(), mts[rt].end(), cmp);
  mts[rt][0].sl = mts[rt][0].b;
  for (int i = 1; i < mts[rt].size(); ++i)
    mts[rt][i].sl = mts[rt][i - 1].sl + mts[rt][i].b;
  mts[rt][mts[rt].size() - 1].sr = mts[rt][mts[rt].size() - 1].a;
  for (int i = mts[rt].size() - 2; i >= 0; --i)
    mts[rt][i].sr = mts[rt][i + 1].sr + mts[rt][i].a;
  if (l == r) return;
  int mid = l + r >> 1;
  build(rt << 1, l, mid);
  build(rt << 1 | 1, mid + 1, r);
}
long long work(int rt, int l, int r, int ti) {
  long long ans = 0;
  if (!tr[rt].op && !tr[rt].nc) {
    it = upper_bound(mts[rt].begin(), mts[rt].end(),
                     (node){0, 0, 0, 0, ti - tr[rt].ti});
    if (it != mts[rt].end())
      ans += (ti - tr[rt].ti) * mts[rt][it - mts[rt].begin()].sr;
    if (it != mts[rt].begin()) ans += mts[rt][it - mts[rt].begin() - 1].sl;
    tr[rt].op = 0;
    tr[rt].ti = ti;
    tr[rt].cvr = 1;
    return ans;
  }
  if (l == r && tr[rt].nc) {
    tr[rt].nc = 0;
    tr[rt].op = 0;
    tr[rt].ti = ti;
    return min(1ll * b[l], 1ll * a[l] * ti + s[l]);
  }
  int mid = l + r >> 1;
  ans = work(rt << 1, l, mid, ti) + work(rt << 1 | 1, mid + 1, r, ti);
  pushup(rt);
  return ans;
}
long long query(int rt, int l, int r, int first, int end, int ti) {
  if (first <= l && r <= end) return work(rt, l, r, ti);
  long long ans = 0;
  pushdown(rt);
  int mid = l + r >> 1;
  if (first <= mid) ans += query(rt << 1, l, mid, first, end, ti);
  if (end > mid) ans += query(rt << 1 | 1, mid + 1, r, first, end, ti);
  pushup(rt);
  return ans;
}
int main() {
  n = read();
  for (int i = 1; i <= n; ++i) {
    s[i] = read(), b[i] = read(), a[i] = read();
    if (a[i])
      c[i] = b[i] / a[i] + (b[i] % a[i] != 0);
    else
      c[i] = 2e9;
  }
  build(1, 1, n);
  int x, y, z;
  m = read();
  while (m--) {
    x = read(), y = read(), z = read();
    cout << query(1, 1, n, y, z, x) << '\n';
  }
  return 0;
}