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>
#pragma GCC optimize("O3")
using namespace std;
const int RLEN = 1 << 20 | 1;
inline char gc() {
  static char ibuf[RLEN], *ib, *ob;
  (ob == ib) && (ob = (ib = ibuf) + fread(ibuf, 1, RLEN, stdin));
  return (ob == ib) ? EOF : *ib++;
}
inline int read() {
  char ch = getchar();
  int res = 0, f = 1;
  while (!isdigit(ch)) f ^= ch == '-', ch = getchar();
  while (isdigit(ch)) res = (res + (res << 2) << 1) + (ch ^ 48), ch = getchar();
  return f ? res : -res;
}
inline void chemx(int &a, int b) { a < b ? a = b : 0; }
inline void chemn(int &a, int b) { a > b ? a = b : 0; }
struct node {
  int s, m, r, t;
  node(int _s = 0, int _m = 0, int _r = 0, int _t = 0)
      : s(_s), m(_m), r(_r), t(_t) {}
  friend inline bool operator<(const node &a, const node &b) {
    return a.t < b.t;
  }
  friend inline bool operator==(const node &a, const node &b) {
    return a.t == b.t;
  }
  friend inline bool operator>(const node &a, const node &b) {
    return a.t > b.t;
  }
  friend inline bool operator<=(const node &a, const node &b) {
    return a.t <= b.t;
  }
  friend inline bool operator>=(const node &a, const node &b) {
    return a.t >= b.t;
  }
  friend inline bool operator!=(const node &a, const node &b) {
    return a.t != b.t;
  }
};
const int N = 100005, inf = 2e9;
node a[N];
int n, q;
struct Tr {
  vector<node> v;
  vector<long long> sl, sr;
  int last;
  inline void init(int l, int r) {
    int len = r - l + 1;
    for (int i = l; i <= r; i++) v.push_back(a[i]);
    sort(v.begin(), v.end());
    sl.resize(len), sr.resize(len);
    sl[0] = v[0].m, sr[len - 1] = v[len - 1].r;
    for (int i = 1; i < len; i++) sl[i] = sl[i - 1] + v[i].m;
    for (int i = len - 2; ~i; i--) sr[i] = sr[i + 1] + v[i].r;
  }
  inline long long query(int t) {
    int tim = t - last;
    int pos =
        lower_bound(v.begin(), v.end(), node(0, 0, 0, tim)) - v.begin() - 1;
    long long res = 0;
    if (~pos) res += sl[pos];
    if (pos + 1 < (int)v.size()) res += sr[pos + 1] * tim;
    last = t;
    return res;
  }
};
namespace Seg {
Tr tr[N << 2];
void build(int u, int l, int r) {
  tr[u].last = -2, tr[u].init(l, r);
  if (l == r) return;
  build((u << 1), l, ((l + r) >> 1)),
      build(((u << 1) | 1), ((l + r) >> 1) + 1, r);
}
inline void pushup(int u) {
  if (tr[(u << 1)].last == tr[((u << 1) | 1)].last)
    if (tr[(u << 1)].last >= 0) {
      tr[u].last = tr[(u << 1)].last;
      return;
    } else if (tr[(u << 1)].last == -2) {
      tr[u].last = -2;
      return;
    }
  tr[u].last = -1;
}
inline void pushdown(int u) {
  if (tr[u].last >= 0) tr[(u << 1)].last = tr[((u << 1) | 1)].last = tr[u].last;
}
long long query(int u, int l, int r, int st, int des, int k) {
  if (l == r && tr[u].last == -2) {
    tr[u].last = k;
    return min(1ll * a[l].m, 1ll * a[l].s + 1ll * a[l].r * k);
  }
  if (st <= l && r <= des && tr[u].last >= 0) return tr[u].query(k);
  long long res = 0;
  pushdown(u);
  if (st <= ((l + r) >> 1))
    res += query((u << 1), l, ((l + r) >> 1), st, des, k);
  if (((l + r) >> 1) < des)
    res += query(((u << 1) | 1), ((l + r) >> 1) + 1, r, st, des, k);
  pushup(u);
  return res;
}
}  // namespace Seg
int main() {
  n = read();
  for (int i = 1; i <= n; i++) {
    a[i].s = read(), a[i].m = read(), a[i].r = read(),
    a[i].t = a[i].r == 0 ? inf : a[i].m / a[i].r;
  }
  Seg::build(1, 1, n);
  q = read();
  while (q--) {
    int t = read(), l = read(), r = read();
    cout << Seg::query(1, 1, n, l, r, t) << '\n';
  }
}