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 up = 100000;
int n, m, s[100050], mx[100050], rp[100050];
int b[100050], c[100050];
long long sum[100050];
pair<long long, long long> operator+(const pair<long long, long long> &a,
                                     const pair<long long, long long> &b) {
  return make_pair(a.first + b.first, a.second + b.second);
}
struct Presidentree {
  bool op;
  int rt[100050], ls[100050 * 20], rs[100050 * 20], node;
  long long val[100050 * 20], sum[100050 * 20];
  void modify(int &x, int las, int l, int r, int pos, int p) {
    x = ++node;
    ls[x] = ls[las];
    rs[x] = rs[las];
    val[x] = val[las] + rp[p];
    sum[x] = sum[las] + mx[p] - (op ? 0 : s[p]);
    if (l == r) return;
    int mid = (l + r) >> 1;
    if (pos <= mid)
      modify(ls[x], ls[las], l, mid, pos, p);
    else
      modify(rs[x], rs[las], mid + 1, r, pos, p);
  }
  pair<long long, long long> query(int a, int b, int l, int r, int L, int R) {
    if (!a) return make_pair(0, 0);
    if (L <= l && r <= R) return make_pair(val[a] - val[b], sum[a] - sum[b]);
    int mid = (l + r) >> 1;
    pair<long long, long long> ans = make_pair(0, 0);
    if (L <= mid) ans = ans + query(ls[a], ls[b], l, mid, L, R);
    if (R > mid) ans = ans + query(rs[a], rs[b], mid + 1, r, L, R);
    return ans;
  }
} T[2];
struct Node {
  int l, r, v;
  Node(int _l = 0, int _r = 0, int _v = 0) : l(_l), r(_r), v(_v) {}
  bool operator<(const Node &a) const { return l < a.l; }
};
set<Node> S;
set<Node>::iterator split(int p) {
  set<Node>::iterator it = S.lower_bound(Node(p));
  if (it != S.end() && it->l == p) return it;
  it--;
  int l = it->l, r = it->r, v = it->v;
  S.erase(it);
  S.insert(Node(l, p - 1, v));
  it = S.lower_bound(Node(l, p - 1, v));
  return S.insert(Node(p, r, v)).first;
}
long long ask(int t, int x, int y) {
  set<Node>::iterator R = split(y + 1), L = split(x);
  long long ans = sum[y] - sum[x - 1];
  for (set<Node>::iterator it = L; it != R; it++) {
    pair<long long, long long> res;
    if (it->v == 0)
      res = T[0].query(T[0].rt[it->r], T[0].rt[it->l - 1], 0, up + 1,
                       min(up + 1, t - it->v + 1), up + 1);
    else
      res = T[1].query(T[1].rt[it->r], T[1].rt[it->l - 1], 0, up + 1,
                       min(up + 1, t - it->v + 1), up + 1);
    ans += 1ll * res.first * (t - it->v) - 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], &mx[i], &rp[i]);
    sum[i] = sum[i - 1] + (long long)mx[i];
    if (rp[i] == 0)
      b[i] = c[i] = up + 1;
    else {
      b[i] = (mx[i] - s[i]) / rp[i] + ((mx[i] - s[i]) % rp[i] > 0);
      c[i] = mx[i] / rp[i] + (mx[i] % rp[i] > 0);
    }
  }
  T[0].op = 0;
  T[1].op = 1;
  for (int i = 1; i <= n; i++)
    T[0].modify(T[0].rt[i], T[0].rt[i - 1], 0, up + 1, b[i], i);
  for (int i = 1; i <= n; i++)
    T[1].modify(T[1].rt[i], T[1].rt[i - 1], 0, up + 1, c[i], i);
  S.insert(Node(1, n, 0));
  S.insert(Node(n + 1, n + 1, 0));
  scanf("%d", &m);
  while (m--) {
    int t, l, r;
    scanf("%d%d%d", &t, &l, &r);
    cout << ask(t, l, r) << '\n';
  }
  return 0;
}