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;
using LL = long long;
namespace _buff {
const size_t BUFF = 1 << 19;
char ibuf[BUFF], *ib = ibuf, *ie = ibuf;
char getc() {
  if (ib == ie) {
    ib = ibuf;
    ie = ibuf + fread(ibuf, 1, BUFF, stdin);
  }
  return ib == ie ? -1 : *ib++;
}
}  // namespace _buff
LL read() {
  using namespace _buff;
  LL ret = 0;
  bool pos = true;
  char c = getc();
  for (; (c < '0' || c > '9') && c != '-'; c = getc()) {
    assert(~c);
  }
  if (c == '-') {
    pos = false;
    c = getc();
  }
  for (; c >= '0' && c <= '9'; c = getc()) {
    ret = (ret << 3) + (ret << 1) + (c ^ 48);
  }
  return pos ? ret : -ret;
}
const size_t N = 1e5 + 5;
int s[N], m[N], r[N];
struct Node {
  int tag = -1;
  vector<tuple<int, LL, LL> > vc;
  Node() {}
} t[N << 2];
void update(int x, int tl, int tr, int val) {
  t[x].tag = val;
  if (val < 0) return;
  Node &n = t[x];
  if (n.vc.empty()) {
    n.vc.emplace_back(0, 0, 0);
    for (int i = tl; i < tr; ++i) {
      n.vc.emplace_back(r[i] ? m[i] / r[i] : INT_MAX, m[i], r[i]);
    }
    sort(n.vc.begin(), n.vc.end());
    for (auto it = n.vc.begin() + 1; it != n.vc.end(); ++it) {
      get<1>(*it) += get<1>(*(it - 1));
      get<2>(*it) += get<2>(*(it - 1));
    }
  }
}
void pushdown(int x, int tl, int tr) {
  if (t[x].tag >= 0) {
    int mid = (tl + tr) >> 1;
    update(((x) << 1), tl, mid, t[x].tag);
    update(((x) << 1 | 1), mid, tr, t[x].tag);
  }
}
LL calc(int x, int tl, int tr, int cur) {
  Node &n = t[x];
  assert(!n.vc.empty());
  auto bnd =
      lower_bound(n.vc.begin(), n.vc.end(), make_tuple(cur - n.tag, 0, 0)) - 1;
  LL ret = get<1>(*bnd) + (cur - n.tag) * (get<2>(n.vc.back()) - get<2>(*bnd));
  update(x, tl, tr, cur);
  return ret;
}
void pushup(int x, int tl, int tr) {
  if (t[((x) << 1)].tag != t[((x) << 1 | 1)].tag) {
    t[x].tag = -1;
  } else {
    update(x, tl, tr, t[((x) << 1)].tag);
  }
}
LL query(int x, int tl, int tr, int l, int r, int cur) {
  if (tr <= l || tl >= r) return 0;
  if (l <= tl && tr <= r && t[x].tag >= 0) {
    return calc(x, tl, tr, cur);
  }
  if (tl + 1 == tr) {
    update(x, tl, tr, cur);
    return min((LL)m[tl], s[tl] + (LL)cur * ::r[tl]);
  }
  pushdown(x, tl, tr);
  int mid = (tl + tr) >> 1;
  LL ret = query(((x) << 1), tl, mid, l, r, cur) +
           query(((x) << 1 | 1), mid, tr, l, r, cur);
  pushup(x, tl, tr);
  return ret;
}
int main() {
  int n = read();
  for (int i = 0; i < n; ++i) {
    s[i] = read();
    m[i] = read();
    r[i] = read();
  }
  int m = read();
  while (m--) {
    int t = read(), l = read() - 1, r = read();
    printf("%lld\n", query(1, 0, n, l, r, t));
  }
  return 0;
}