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 baseFastio {
template <typename Int>
inline Int read() {
  Int flag = 1;
  char c = getchar();
  while ((!isdigit(c)) && c != '-' && c != EOF) c = getchar();
  if (c == '-') flag = -1, c = getchar();
  Int init = c & 15;
  while (isdigit(c = getchar())) init = (init << 3) + (init << 1) + (c & 15);
  return init * flag;
}
template <typename Int>
inline Int read(char& c) {
  Int flag = 1;
  c = getchar();
  while ((!isdigit(c)) && c != '-' && c != EOF) c = getchar();
  if (c == '-') flag = -1, c = getchar();
  Int init = c & 15;
  while (isdigit(c = getchar())) init = (init << 3) + (init << 1) + (c & 15);
  return init * flag;
}
template <typename Int>
inline void write(Int x) {
  if (x < 0) putchar('-'), x = ~x + 1;
  if (x > 9) write(x / 10);
  putchar((x % 10) | 48);
}
template <typename Int>
inline void write(Int x, char nextch) {
  write(x);
  putchar(nextch);
}
}  // namespace baseFastio
namespace Fastio {
enum io_flags { ignore_int = 1 << 0 };
struct Reader {
  char endch;
  Reader() { endch = '\0'; }
  template <typename Int>
  Int operator()() {
    return baseFastio::read<Int>(endch);
    ;
  }
  Reader& operator>>(io_flags f) {
    if (f == ignore_int) baseFastio::read<int>();
    return *this;
  }
  template <typename Int>
  Reader& operator>>(Int& i) {
    i = baseFastio::read<Int>(endch);
    return *this;
  }
  template <typename Int>
  inline Int get_int() {
    return baseFastio::read<Int>();
  }
  inline char get_nxt() { return endch; }
} read;
struct Writer {
  Writer& operator<<(const char* ch) {
    while (*ch) putchar(*(ch++));
    return *this;
  }
  Writer& operator<<(const char ch) {
    putchar(ch);
    return *this;
  }
  template <typename Int>
  Writer& operator<<(const Int i) {
    baseFastio::write(i);
    return *this;
  }
} write;
}  // namespace Fastio
using namespace Fastio;
const int MY_INT_MAX = INT_MAX / 3;
const int N = 1e5 + 8, MY_GREATER_THAN_MAXN = 1e5 + 3;
namespace against_cpp11 {
int n, m, S[N], M[N], R[N], b[N], c[N];
long long sumM[N];
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 ChairmanTree {
  int op, rt[N], ls[N * 20], rs[N * 20], node_count;
  long long val[N * 20], sum[N * 20], w[N * 20];
  void update(int& x, int y, int l, int r, int pos, int p) {
    x = ++node_count;
    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;
  }
} T0, T1;
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<(const node& b) const { return l < b.l; }
};
set<node> s;
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 = T0.query(T0.rt[l - 1], T0.rt[r],
                     min(MY_GREATER_THAN_MAXN, t - val + 1),
                     MY_GREATER_THAN_MAXN, 0, MY_GREATER_THAN_MAXN);
    else
      res = T1.query(T1.rt[l - 1], T1.rt[r],
                     min(MY_GREATER_THAN_MAXN, t - val + 1),
                     MY_GREATER_THAN_MAXN, 0, MY_GREATER_THAN_MAXN);
    ans += res.first * (t - val) - res.second;
  }
  s.erase(L, R);
  s.insert(node(x, y, t));
  return ans;
}
int main() {
  read >> n;
  for (int i = 1; i <= n; i++) {
    read >> S[i] >> M[i] >> R[i];
    b[i] = (R[i] == 0) ? MY_GREATER_THAN_MAXN
                       : (M[i] - S[i]) / R[i] + ((M[i] - S[i]) % R[i] > 0);
    c[i] = (R[i] == 0) ? MY_GREATER_THAN_MAXN : M[i] / R[i] + (M[i] % R[i] > 0);
    sumM[i] = sumM[i - 1] + M[i];
  }
  T0.op = 0;
  T1.op = 1;
  for (int i = 1; i <= n; i++)
    T0.rt[i] = T0.rt[i - 1],
    T0.update(T0.rt[i], T0.rt[i], 0, MY_GREATER_THAN_MAXN, b[i], i);
  for (int i = 1; i <= n; i++)
    T1.rt[i] = T1.rt[i - 1],
    T1.update(T1.rt[i], T1.rt[i], 0, MY_GREATER_THAN_MAXN, c[i], i);
  s.insert(node(1, n, 0));
  s.insert(node(n + 1, n + 1, 0));
  read >> m;
  for (int i = 1, t, l, r; i <= m; i++) {
    read >> t >> l >> r;
    write << ask(t, l, r) << '\n';
  }
  return 0;
}
}  // namespace against_cpp11
signed main() { return against_cpp11::main(); }