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 inf = 0x3f3f3f3f;
template <typename T>
inline void read(T &x) {
  x = 0;
  T fl = 1;
  char ch = 0;
  for (; ch < '0' || ch > '9'; ch = getchar())
    if (ch == '-') fl = -1;
  for (; ch >= '0' && ch <= '9'; ch = getchar())
    x = (x << 1) + (x << 3) + (ch ^ 48);
  x *= fl;
}
template <typename T, typename... Args>
inline void read(T &x, Args &...args) {
  read(x);
  read(args...);
}
const int N = 1e5 + 5;
struct data {
  int s, m, r, t;
  data(int x = 0) { t = x; }
  bool operator<(const data &b) const { return t < b.t; }
  long long calc(long long tim) { return min(s + tim * r, m * 1ll); }
} a[18][N];
struct seg {
  int trt[N << 2];
  long long trm[18][N << 2], trr[18][N << 2];
  void pushdown(int nod) {
    if (trt[nod] >= 0) trt[(nod << 1)] = trt[(nod << 1 | 1)] = trt[nod];
  }
  void pushup(int nod) {
    if (trt[(nod << 1)] != trt[(nod << 1 | 1)])
      trt[nod] = -1;
    else
      trt[nod] = trt[(nod << 1)];
  }
  void build(int nod, int l, int r, int dep) {
    trt[nod] = -2;
    if (l == r) {
      read(a[dep][l].s, a[dep][l].m, a[dep][l].r);
      if (a[dep][l].r == 0)
        a[dep][l].t = inf;
      else
        a[dep][l].t = a[dep][l].m % a[dep][l].r == 0
                          ? a[dep][l].m / a[dep][l].r
                          : a[dep][l].m / a[dep][l].r + 1;
      trm[dep][l] = trm[dep][l - 1] + a[dep][l].m;
      trr[dep][l] = trr[dep][l - 1] + a[dep][l].r;
      return;
    }
    int mid = (l + r) >> 1;
    build((nod << 1), l, mid, dep + 1);
    build((nod << 1 | 1), mid + 1, r, dep + 1);
    for (int i = (l); i <= (r); i++) a[dep][i] = a[dep + 1][i];
    sort(a[dep] + l, a[dep] + r + 1);
    for (int i = (l); i <= (r); i++) {
      trm[dep][i] = trm[dep][i - 1] + a[dep][i].m;
      trr[dep][i] = trr[dep][i - 1] + a[dep][i].r;
    }
  }
  long long query(int tim, int nod, int l, int r, int ql, int qr, int dep) {
    long long res = 0;
    if (ql <= l && r <= qr && trt[nod] != -1) {
      if (trt[nod] == -2)
        for (int i = (l); i <= (r); i++) res += a[dep][i].calc(tim);
      else {
        int i = upper_bound(a[dep] + l, a[dep] + 1 + r, data(tim - trt[nod])) -
                a[dep] - 1;
        res = (tim - trt[nod]) * (trr[dep][r] - trr[dep][i]) + trm[dep][i] -
              trm[dep][l - 1];
      }
      trt[nod] = tim;
      return res;
    }
    pushdown(nod);
    int mid = (l + r) >> 1;
    if (ql <= mid) res += query(tim, (nod << 1), l, mid, ql, qr, dep + 1);
    if (qr > mid)
      res += query(tim, (nod << 1 | 1), mid + 1, r, ql, qr, dep + 1);
    pushup(nod);
    return res;
  }
} sgt;
int n, m;
int main() {
  read(n);
  sgt.build(1, 1, n, 0);
  read(m);
  for (int _ = (1); _ <= (m); _++) {
    int t, l, r;
    read(t);
    read(l);
    read(r);
    printf("%lld\n", sgt.query(t, 1, 1, n, l, r, 0));
  }
  return 0;
}