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 maxn = 100000 + 10;
const int inf = 0x3f3f3f3f;
int n, m, S[maxn], M[maxn], R[maxn], b[maxn], c[maxn];
long long sumM[maxn];
inline pair<long long, long long> operator+(pair<long long, long long> a,
                                            pair<long long, long long> b) {
  return make_pair(a.first + b.first, a.second + b.second);
}
struct CM {
  int op, rt[maxn], ls[maxn * 20], rs[maxn * 20], sz;
  long long val[maxn * 20], sum[maxn * 20], w[maxn * 20];
  void update(int &x, int y, int l, int r, int pos, int p) {
    x = ++sz;
    ls[x] = ls[y];
    rs[x] = rs[y];
    val[x] = val[y] + R[p];
    sum[x] = sum[y] + M[p] - ((op == 0) ? S[p] : 0);
    if (l == r) return;
    int mid = (l + r) >> 1;
    if (pos <= mid)
      update(ls[x], ls[y], l, mid, pos, p);
    else
      update(rs[x], rs[y], mid + 1, r, pos, p);
  }
  pair<long long, long long> query(int u, int v, int L, int R, int l, int r) {
    if (L <= l && r <= R) return make_pair(val[v] - val[u], sum[v] - sum[u]);
    int mid = (l + r) >> 1;
    pair<long long, long long> ans = make_pair(0, 0);
    if (L <= mid) ans = ans + query(ls[u], ls[v], L, R, l, mid);
    if (R > mid) ans = ans + query(rs[u], rs[v], L, R, mid + 1, r);
    return ans;
  }
} T[2];
struct node {
  int l, r, val;
  node(int _l = 0, int _r = 0, int _val = 0) : l(_l), r(_r), val(_val) {}
};
set<node> s;
inline bool operator<(const node &a, const node &b) { return a.l < b.l; }
inline set<node>::iterator split(int x) {
  set<node>::iterator it = s.lower_bound(node(x));
  if (it != s.end() && it->l == x) return it;
  it--;
  int l = it->l, r = it->r, val = it->val;
  s.erase(it);
  s.insert(node(l, x - 1, val));
  return s.insert(node(x, r, val)).first;
}
inline long long ask(int t, int x, int y) {
  set<node>::iterator R = split(y + 1), L = split(x);
  int l, r, val;
  long long ans = sumM[y] - sumM[x - 1];
  pair<long long, long long> res;
  for (set<node>::iterator it = L; it != R; it++) {
    l = it->l;
    r = it->r;
    val = it->val;
    if (val == 0)
      res = T[0].query(T[0].rt[l - 1], T[0].rt[r], min(100001, t - val + 1),
                       100001, 0, 100001);
    else
      res = T[1].query(T[1].rt[l - 1], T[1].rt[r], min(100001, t - val + 1),
                       100001, 0, 100001);
    ans += res.first * (t - val) - res.second;
  }
  s.erase(L, R);
  s.insert(node(x, y, t));
  return ans;
}
int main() {
  scanf("%d", &n);
  for (int i = 1; i <= n; i++) scanf("%d%d%d", &S[i], &M[i], &R[i]);
  for (int i = 1; i <= n; i++) {
    b[i] = (R[i] == 0) ? 100001
                       : (M[i] - S[i]) / R[i] + ((M[i] - S[i]) % R[i] > 0);
    c[i] = (R[i] == 0) ? 100001 : M[i] / R[i] + (M[i] % R[i] > 0);
    sumM[i] = sumM[i - 1] + M[i];
  }
  T[0].op = 0;
  T[1].op = 1;
  for (int i = 1; i <= n; i++)
    T[0].rt[i] = T[0].rt[i - 1],
    T[0].update(T[0].rt[i], T[0].rt[i], 0, 100001, b[i], i);
  for (int i = 1; i <= n; i++)
    T[1].rt[i] = T[1].rt[i - 1],
    T[1].update(T[1].rt[i], T[1].rt[i], 0, 100001, c[i], i);
  s.insert(node(1, n, 0));
  s.insert(node(n + 1, n + 1, 0));
  scanf("%d", &m);
  int t, l, r;
  while (m--) {
    scanf("%d%d%d", &t, &l, &r);
    printf("%I64d\n", ask(t, l, r));
  }
  return 0;
}