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;
const int N = 200010, INF = 0x3f3f3f3f;
int n, m;
struct P {
  long long S, M, R, T;
};
P a[N];
long long S[N], M[N], R[N], T[N];
int flag[N * 4];
vector<int> t[N * 4];
vector<P> pp[N * 4];
vector<long long> pre[N * 4], suf[N * 4];
inline long long Min(const long long &x, const long long &y) {
  return x < y ? x : y;
}
void pushup(int u) {
  pp[u].clear(), t[u].clear(), pre[u].clear(), suf[u].clear();
  int ls = u * 2, rs = u * 2 + 1;
  int n1 = pp[ls].size(), n2 = pp[rs].size(), p1 = 0, p2 = 0;
  for (; p1 < n1 && p2 < n2;) {
    if (pp[ls][p1].T < pp[rs][p2].T)
      pp[u].push_back(pp[ls][p1]), ++p1;
    else
      pp[u].push_back(pp[rs][p2]), ++p2;
  }
  for (; p1 < n1;) pp[u].push_back(pp[ls][p1]), ++p1;
  for (; p2 < n2;) pp[u].push_back(pp[rs][p2]), ++p2;
  long long Pre = 0, Suf = 0;
  for (int i = 0; i < pp[u].size(); i++)
    Pre += pp[u][i].M, pre[u].push_back(Pre), t[u].push_back(pp[u][i].T);
  suf[u].resize(pp[u].size());
  for (int i = pp[u].size() - 1; ~i; i--) Suf += pp[u][i].R, suf[u][i] = Suf;
}
void build(int u, int l, int r) {
  flag[u] = -2;
  if (l >= r) {
    pp[u].push_back(a[l]), pre[u].push_back(a[l].M);
    t[u].push_back(a[l].T), suf[u].push_back(a[l].R);
    return;
  }
  int mid = (l + r) >> 1;
  build(u * 2, l, mid), build(u * 2 + 1, mid + 1, r), pushup(u);
}
long long get(int u, long long tt) {
  tt -= flag[u];
  int pos = lower_bound(t[u].begin(), t[u].end(), tt) - t[u].begin();
  long long ss = 0;
  if (pos) ss += pre[u][pos - 1];
  if (pos != t[u].size()) ss += suf[u][pos] * tt;
  return ss;
}
void pushdown(int u) {
  if (flag[u] >= 0) flag[u * 2] = flag[u * 2 + 1] = flag[u];
}
long long qry(int u, int l, int r, int L, int R, long long tt) {
  if (l >= r && flag[u] == -2) {
    long long ss = a[l].S;
    ss += a[l].R * tt, ss = Min(ss, a[l].M);
    flag[u] = (int)tt;
    return ss;
  }
  if (L <= l && r <= R && flag[u] >= 0) {
    long long ss = get(u, tt);
    flag[u] = (int)tt;
    return ss;
  }
  int mid = (l + r) >> 1;
  pushdown(u);
  long long ss = 0;
  if (L <= mid) ss += qry(u * 2, l, mid, L, R, tt);
  if (R > mid) ss += qry(u * 2 + 1, mid + 1, r, L, R, tt);
  if (flag[u * 2] == -2 || flag[u * 2 + 1] == -2)
    flag[u] = -2;
  else if (flag[u * 2] == flag[u * 2 + 1] && flag[u * 2] >= 0)
    flag[u] = flag[u * 2];
  else
    flag[u] = -1;
  return ss;
}
int main() {
  scanf("%d", &n);
  for (int i = 1; i <= n; i++) {
    scanf("%I64d%I64d%I64d", &a[i].S, &a[i].M, &a[i].R);
    if (a[i].R)
      a[i].T = a[i].M / a[i].R;
    else
      a[i].T = INF;
  }
  build(1, 1, n), scanf("%d", &m);
  for (int tt, xx, yy; m; --m) {
    scanf("%d%d%d", &tt, &xx, &yy);
    printf("%I64d\n", qry(1, 1, n, xx, yy, (long long)tt));
  }
  return 0;
}