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 std::cerr;
using std::endl;
const int N = 1e5 + 5, INF = 1e9;
int n, m;
struct Horse {
  int s, m, r, t;
} A[N];
struct Node {
  std::vector<long long> sum, add;
  std::vector<int> ti;
  std::vector<Horse> h;
  int tag;
  inline void merge(const std::vector<Horse> &l, const std::vector<Horse> &r) {
    int n1 = l.size(), n2 = r.size(), p1 = 0, p2 = 0;
    while (p1 < n1 || p2 < n2) {
      if (p2 >= n2 || (p1 < n1 && l[p1].t <= r[p2].t)) {
        h.push_back(l[p1++]);
      } else {
        h.push_back(r[p2++]);
      }
    }
    long long s = 0, a = 0;
    for (int i = 0; i < n1 + n2; ++i) {
      s += h[i].m;
      sum.push_back(s);
      a += h[i].r;
      add.push_back(a);
      ti.push_back(h[i].t);
    }
  }
  inline long long calc(int t) {
    t -= tag;
    int p = std::lower_bound(ti.begin(), ti.end(), t) - ti.begin();
    long long ret = 0, tmp = 0;
    if (p > 0) ret += sum[p - 1], tmp -= add[p - 1];
    tmp += add.back();
    ret += tmp * t;
    return ret;
  }
} t[N * 4];
inline void pushup(int p) {
  t[p].tag = -1;
  if (t[(p << 1)].tag > 0 && t[(p << 1)].tag == t[((p << 1) | 1)].tag)
    t[p].tag = t[(p << 1)].tag;
}
inline void pushdown(int p) {
  if (t[p].tag > 0) {
    t[(p << 1)].tag = t[((p << 1) | 1)].tag = t[p].tag;
  }
}
void build(int p, int L, int R) {
  t[p].tag = -1;
  if (L == R) {
    t[p].h.push_back(A[L]);
    t[p].add.push_back(A[L].r);
    t[p].sum.push_back(A[L].m);
    t[p].ti.push_back(A[L].t);
    return;
  }
  build((p << 1), L, ((L + R) >> 1));
  build(((p << 1) | 1), ((L + R) >> 1) + 1, R);
  t[p].merge(t[(p << 1)].h, t[((p << 1) | 1)].h);
}
long long query(int p, int l, int r, int tm, int L, int R) {
  if (l <= L && r >= R && t[p].tag > 0) {
    long long ret = t[p].calc(tm);
    t[p].tag = tm;
    return ret;
  }
  if (L == R) {
    long long ret = A[L].s + 1ll * tm * A[L].r;
    ret = std::min(ret, (long long)A[L].m);
    t[p].tag = tm;
    return ret;
  }
  pushdown(p);
  long long ret = 0;
  if (l <= ((L + R) >> 1)) ret += query((p << 1), l, r, tm, L, ((L + R) >> 1));
  if (r > ((L + R) >> 1))
    ret += query(((p << 1) | 1), l, r, tm, ((L + R) >> 1) + 1, R);
  pushup(p);
  return ret;
}
int main() {
  std::ios::sync_with_stdio(0);
  std::cin.tie(0);
  std::cin >> n;
  for (int i = 1; i <= n; ++i) {
    std::cin >> A[i].s >> A[i].m >> A[i].r;
    A[i].t = A[i].r ? A[i].m / A[i].r : INF;
  }
  build(1, 1, n);
  std::cin >> m;
  while (m--) {
    int tm, l, r;
    std::cin >> tm >> l >> r;
    long long ans = query(1, l, r, tm, 1, n);
    std::cout << ans << '\n';
  }
  return 0;
}