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;
template <typename T>
inline T read(register T& t) {
  register T f = 1;
  register char ch = getchar();
  t = 0;
  while (ch < '0' || ch > '9') {
    if (ch == '-') f = -f;
    ch = getchar();
  }
  while (ch >= '0' && ch <= '9') t = t * 10 + ch - '0', ch = getchar();
  t *= f;
  return t;
}
template <typename T, typename... Args>
inline void read(T& t, Args&... args) {
  read(t);
  read(args...);
}
const long long p = 998244353;
inline long long power(register long long x, register long long k = p - 2) {
  register long long re = 1;
  for (; k; k >>= 1, x = x * x % p)
    if (k & 1) re = re * x % p;
  return re;
}
int n;
long long S[100005], R[100005], K[100005];
struct SGT {
  int lcs, rcs;
  long long Tag;
  vector<int> Arr;
  vector<long long> Sr, Sk;
} v[200005];
int tot;
inline bool cmp(int x, int y) {
  long long tx = K[x] ? (R[x] / K[x] + (R[x] % K[x] > 0)) : 1e18;
  long long ty = K[y] ? (R[y] / K[y] + (R[y] % K[y] > 0)) : 1e18;
  return tx < ty;
}
void build(int l, int r, int& p) {
  p = ++tot;
  for (int i = l; i <= r; i++) v[p].Arr.push_back(i);
  sort(v[p].Arr.begin(), v[p].Arr.end(), cmp);
  long long SumR = 0;
  for (int i : v[p].Arr) v[p].Sr.push_back(SumR += R[i]);
  long long SumK = 0;
  for (int i : v[p].Arr) v[p].Sk.push_back(SumK += K[i]);
  v[p].Tag = -2;
  if (l == r) return;
  build(l, ((l + r) >> 1), v[p].lcs);
  build(((l + r) >> 1) + 1, r, v[p].rcs);
}
void push_down(int l, int r, int p) {
  if (v[p].Tag >= 0) {
    v[v[p].lcs].Tag = v[p].Tag;
    v[v[p].rcs].Tag = v[p].Tag;
  }
}
long long enquire(int l, int r, int p, int nl, int nr, long long T) {
  if (nl <= l && r <= nr && v[p].Tag != -1) {
    if (v[p].Tag == -2) {
      v[p].Tag = T;
      long long ans = 0;
      for (int i = l; i <= r; i++) ans += min(T * K[i] + S[i], R[i]);
      return ans;
    }
    R[0] = T - v[p].Tag;
    K[0] = 1;
    int id = upper_bound(v[p].Arr.begin(), v[p].Arr.end(), 0, cmp) -
             v[p].Arr.begin();
    long long A = 0, B = 0;
    if (id != 0) A = v[p].Sr[id - 1];
    if (id != 0)
      B = (*v[p].Sk.rbegin() - v[p].Sk[id - 1]) * (T - v[p].Tag);
    else
      B = *v[p].Sk.rbegin() * (T - v[p].Tag);
    v[p].Tag = T;
    return A + B;
  }
  push_down(l, r, p);
  long long re = 0;
  if (nl <= ((l + r) >> 1))
    re += enquire(l, ((l + r) >> 1), v[p].lcs, nl, nr, T);
  if (((l + r) >> 1) + 1 <= nr)
    re += enquire(((l + r) >> 1) + 1, r, v[p].rcs, nl, nr, T);
  if (v[v[p].lcs].Tag == v[v[p].rcs].Tag)
    v[p].Tag = v[v[p].lcs].Tag;
  else
    v[p].Tag = -1;
  return re;
}
int main() {
  read(n);
  for (int i = 1; i <= n; i++) read(S[i], R[i], K[i]);
  int root;
  build(1, n, root);
  int Q;
  read(Q);
  while (Q--) {
    int T, l, r;
    read(T, l, r);
    printf("%lld\n", enquire(1, n, 1, l, r, T));
  }
  return 0;
}