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 std::max;
using std::min;
const int inf = 0x3f3f3f3f, Inf = 0x7fffffff;
const long long INF = 0x3f3f3f3f3f3f3f3f;
std::mt19937 rnd(std::chrono::steady_clock::now().time_since_epoch().count());
template <typename _Tp>
_Tp gcd(const _Tp &a, const _Tp &b) {
  return (!b) ? a : gcd(b, a % b);
}
template <typename _Tp>
inline _Tp abs(const _Tp &a) {
  return a >= 0 ? a : -a;
}
template <typename _Tp>
inline void chmax(_Tp &a, const _Tp &b) {
  (a < b) && (a = b);
}
template <typename _Tp>
inline void chmin(_Tp &a, const _Tp &b) {
  (b < a) && (a = b);
}
template <typename _Tp>
inline void read(_Tp &x) {
  char ch(getchar());
  bool f(false);
  while (!isdigit(ch)) f |= ch == 45, ch = getchar();
  x = ch & 15, ch = getchar();
  while (isdigit(ch)) x = (((x << 2) + x) << 1) + (ch & 15), ch = getchar();
  f && (x = -x);
}
template <typename _Tp, typename... Args>
inline void read(_Tp &t, Args &...args) {
  read(t);
  read(args...);
}
inline int read_str(char *s) {
  char ch(getchar());
  while (ch == ' ' || ch == '\r' || ch == '\n') ch = getchar();
  char *tar = s;
  *tar++ = ch, ch = getchar();
  while (ch != ' ' && ch != '\r' && ch != '\n' && ch != EOF)
    *tar++ = ch, ch = getchar();
  return *tar = 0, tar - s;
}
const int N = 100005;
struct seg_tr {
  int ls[N << 6], rs[N << 6], node_cnt;
  long long sum0[N << 6], sum1[N << 6];
  int build(int l, int r) {
    int cur = ++node_cnt;
    if (l == r) return cur;
    int mid = (l + r) >> 1;
    ls[cur] = build(l, mid), rs[cur] = build(mid + 1, r);
    return cur;
  }
  void Update(int &cur, int type, int pos, int C, int l, int r) {
    int tmp = ++node_cnt;
    ls[tmp] = ls[cur], rs[tmp] = rs[cur], sum1[tmp] = sum1[cur],
    sum0[tmp] = sum0[cur], cur = tmp;
    (type ? sum1[cur] : sum0[cur]) += C;
    if (l == r) return;
    int mid = (l + r) >> 1;
    pos <= mid ? Update(ls[cur], type, pos, C, l, mid)
               : Update(rs[cur], type, pos, C, mid + 1, r);
  }
  long long Query(int L, int R, int type, int u, int v, int l, int r) {
    if (L <= l && r <= R) return type ? sum1[v] - sum1[u] : sum0[v] - sum0[u];
    int mid = (l + r) >> 1;
    if (L <= mid && R > mid)
      return Query(L, R, type, ls[u], ls[v], l, mid) +
             Query(L, R, type, rs[u], rs[v], mid + 1, r);
    return L <= mid ? Query(L, R, type, ls[u], ls[v], l, mid)
                    : Query(L, R, type, rs[u], rs[v], mid + 1, r);
  }
} tr;
int s[N], m[N], r[N], root[N];
struct frac {
  long long a, b;
  inline bool operator==(const frac &o) const { return a * o.b == o.a * b; }
  inline bool operator<(const frac &o) const { return a * o.b < o.a * b; }
} T[N];
struct node {
  int l, r;
  mutable int t;
  inline bool operator<(const node &o) const { return l < o.l; }
};
std::set<node> S;
std::set<node>::iterator split(int x) {
  auto it = S.lower_bound({x, x, 0});
  if (it != S.end() && it->l == x) return it;
  node val = *--it;
  if (val.r < x) return S.end();
  return S.erase(it), S.insert({val.l, x - 1, val.t}),
         S.insert({x, val.r, val.t}).first;
}
int main() {
  int n;
  read(n);
  int pos = 0;
  for (int i = 1; i <= n; ++i) read(s[i], m[i], r[i]), T[++pos] = {m[i], r[i]};
  std::sort(T + 1, T + pos + 1), pos = std::unique(T + 1, T + pos + 1) - T - 1;
  root[0] = tr.build(1, pos);
  for (int i = 1; i <= n; ++i) {
    int id = std::lower_bound(T + 1, T + pos + 1, (frac){m[i], r[i]}) - T;
    root[i] = root[i - 1], tr.Update(root[i], 0, id, r[i], 1, pos),
    tr.Update(root[i], 1, id, m[i], 1, pos);
  }
  for (int i = 1; i <= n; ++i) S.insert({i, i, -1});
  int q;
  read(q);
  while (q--) {
    int t, l, r;
    read(t, l, r);
    auto itr = split(r + 1), itl = split(l);
    long long ans = 0;
    for (auto it = itl; it != itr; ++it) {
      if (it->t == -1)
        ans += min(s[it->l] + 1LL * t * ::r[it->l], (long long)m[it->l]),
            it->t = 0, s[it->l] = 0;
      else {
        int cur = t - it->t,
            p = std::upper_bound(T + 1, T + pos + 1, (frac){cur, 1}) - T - 1;
        if (1 <= p)
          ans += tr.Query(1, p, 1, root[it->l - 1], root[it->r], 1, pos);
        if (p < pos)
          ans += tr.Query(p + 1, pos, 0, root[it->l - 1], root[it->r], 1, pos) *
                 cur;
      }
    }
    S.erase(itl, itr), S.insert({l, r, t});
    printf("%lld\n", ans);
  }
  return 0;
}