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>
const int MAXN = 100010;
std::vector<std::pair<int, int> > add[MAXN], rem[MAXN];
int n, m;
int ss[MAXN], ms[MAXN], rs[MAXN];
const int UP = 1e5 + 5;
std::map<int, std::set<int> > S;
long long trm[MAXN], trv[MAXN];
void addmul(int x, int v) {
  for (; x; x &= x - 1) trm[x] += v;
}
void addval(int x, long long v) {
  for (; x <= UP; x += x & -x) trv[x] += v;
}
long long qrymul(int x) {
  long long r = 0;
  for (; x <= UP; x += x & -x) r += trm[x];
  return r;
}
long long qryval(int x) {
  long long r = 0;
  for (; x; x &= x - 1) r += trv[x];
  return r;
}
int at[MAXN];
long long bm[MAXN], bv[MAXN], ansl[MAXN];
void ins(int x, int L) {
  at[x] = std::min(L, UP);
  bm[x] = qrymul(at[x]);
  bv[x] = qryval(at[x]);
}
void del(int x) {
  if (!at[x]) return;
  bm[x] = qrymul(at[x]) - bm[x];
  bv[x] = qryval(at[x]) - bv[x];
  ansl[x] += bv[x] + at[x] * bm[x];
  at[x] = 0;
}
void _upd(std::map<int, std::set<int> >::iterator x, int v) {
  if (x == S.begin()) return;
  int id = *(x->second.begin());
  if (v == -1)
    del(id);
  else
    ins(id, x->first - std::prev(x)->first);
}
void update(int x, int v) {
  auto it = S.lower_bound(x);
  if (it == S.end()) return;
  _upd(it, v);
  if (it->first == x && ++it != S.end()) _upd(it, v);
}
void mdf(int x, int v, int id) {
  update(x, -1);
  if (v == 1)
    S[x].insert(id);
  else {
    if (S[x].size() == 1)
      S.erase(x);
    else
      S[x].erase(id);
  }
  update(x, 1);
}
int main() {
  std::ios_base::sync_with_stdio(false), std::cin.tie(0);
  std::cin >> n;
  for (int i = 1; i <= n; ++i) std::cin >> ss[i] >> ms[i] >> rs[i];
  std::cin >> m;
  for (int i = 1, t, l, r; i <= m; ++i) {
    std::cin >> t >> l >> r;
    add[l].emplace_back(t, i);
    rem[r + 1].emplace_back(t, i);
  }
  for (int i = 1; i <= n; ++i) {
    for (auto t : add[i]) mdf(t.first, 1, t.second);
    for (auto t : rem[i]) mdf(t.first, -1, t.second);
    if (S.empty()) continue;
    int at = S.begin()->first, tar = *(S.begin()->second.begin());
    ansl[tar] += std::min<long long>((long long)at * rs[i] + ss[i], ms[i]);
    if (rs[i] && ms[i]) {
      int br = (ms[i] + rs[i] - 1) / rs[i];
      addmul(br - 1, rs[i]);
      addval(br, ms[i]);
    }
  }
  for (int i = 1; i <= m; ++i) del(i);
  for (int i = 1; i <= m; ++i) std::cout << ansl[i] << '\n';
  return 0;
}