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;
namespace BlueQuantum {
int const N = 1e5 + 5;
int n, q, s[N], m[N], k[N], b[N], c[N];
long long sum[N];
inline pair<long long, long long> operator+(
    pair<long long, long long> const &lhs,
    pair<long long, long long> const &rhs) {
  return make_pair(lhs.first + rhs.first, lhs.second + rhs.second);
}
struct PersistentSegTree {
  struct Node {
    int ls, rs;
    long long val, sum;
  };
  int op, rt[N], cnt;
  Node tr[N * 20];
  void modify(int &x, int y, int l, int r, int pos, int v) {
    tr[x = ++cnt] = tr[y];
    tr[x].val += k[v];
    tr[x].sum = tr[y].sum + m[v] - ((op == 0) ? s[v] : 0);
    if (l == r) return;
    int mid = (l + r) >> 1;
    if (pos <= mid)
      modify(tr[x].ls, tr[y].ls, l, mid, pos, v);
    else
      modify(tr[x].rs, tr[y].rs, mid + 1, r, pos, v);
  }
  pair<long long, long long> query(int x, int y, int L, int R, int l, int r) {
    if (L <= l && r <= R)
      return make_pair(tr[y].val - tr[x].val, tr[y].sum - tr[x].sum);
    int mid = (l + r) >> 1;
    pair<long long, long long> ans(0, 0);
    if (L <= mid) ans = ans + query(tr[x].ls, tr[y].ls, L, R, l, mid);
    if (R > mid) ans = ans + query(tr[x].rs, tr[y].rs, 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) {}
  inline bool operator<(Node const &rhs) const { return l < rhs.l; }
};
set<Node> odt;
inline set<Node>::iterator split(int x) {
  set<Node>::iterator it = odt.lower_bound(Node(x));
  if (it != odt.end() && it->l == x) return it;
  --it;
  int l = it->l, r = it->r, val = it->val;
  odt.erase(it);
  odt.insert(Node(l, x - 1, val));
  return odt.insert(Node(x, r, val)).first;
}
inline long long query(int t, int x, int y) {
  set<Node>::iterator rit = split(y + 1), lit = split(x);
  int l, r, val;
  long long ans = sum[y] - sum[x - 1];
  pair<long long, long long> res;
  for (set<Node>::iterator it = lit; it != rit; ++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(N, t - val + 1), N, 0, N);
    else
      res =
          T[1].query(T[1].rt[l - 1], T[1].rt[r], min(N, t - val + 1), N, 0, N);
    ans += res.first * (t - val) - res.second;
  }
  odt.erase(lit, rit);
  odt.insert(Node(x, y, t));
  return ans;
}
inline int main() {
  cin >> n;
  for (int i = 1; i <= n; ++i) cin >> s[i] >> m[i] >> k[i];
  for (int i = 1; i <= n; ++i) {
    b[i] = (k[i] == 0) ? N : (m[i] - s[i]) / k[i] + ((m[i] - s[i]) % k[i] > 0);
    c[i] = (k[i] == 0) ? N : m[i] / k[i] + (m[i] % k[i] > 0);
    sum[i] = sum[i - 1] + m[i];
  }
  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, N, b[i], i);
  for (int i = 1; i <= n; ++i)
    T[1].modify(T[1].rt[i], T[1].rt[i - 1], 0, N, c[i], i);
  odt.insert(Node(1, n, 0));
  cin >> q;
  for (int t, l, r; q; --q) {
    cin >> t >> l >> r;
    cout << query(t, l, r) << '\n';
  }
  return 0;
}
}  // namespace BlueQuantum
int main() {
  ios::sync_with_stdio(false), cin.tie(NULL), cout.tie(NULL);
  return BlueQuantum::main();
}