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;
int n, q;
long long b[((int)101 * 1000)];
int m[((int)101 * 1000)], a[((int)101 * 1000)],
    t_lst[((int)((int)101 * 1000) / ((int)400) + 50)];
pair<long long, int> arr[((int)101 * 1000)];
int prt_m[((int)101 * 1000)], prt_a[((int)101 * 1000)];
bool all_same[((int)((int)101 * 1000) / ((int)400) + 50)];
void reset(int x) {
  int l = x * ((int)400), r = min(n, (x + 1) * ((int)400));
  for (int i = l; i < r; i++)
    arr[i] = {(a[i] == 0) ? ((long long)1e18) : (m[i] + a[i] - 1) / a[i], i};
  sort(arr + l, arr + r);
  prt_m[l] = m[arr[l].second];
  prt_a[l] = a[arr[l].second];
  for (int i = l + 1; i < r; i++)
    prt_m[i] = prt_m[i - 1] + m[arr[i].second],
    prt_a[i] = prt_a[i - 1] + a[arr[i].second];
}
int main() {
  scanf("%d", &n);
  for (int i = 0; i < n; i++) {
    int ex1, ex2, ex3;
    scanf("%d%d%d", &ex1, &ex2, &ex3);
    b[i] = ex1;
    m[i] = ex2;
    a[i] = ex3;
  }
  for (int i = 0; i < (n + ((int)400) - 1) / ((int)400); i++) reset(i);
  scanf("%d", &q);
  while (q--) {
    int t, l, r;
    long long ans = 0;
    scanf("%d%d%d", &t, &l, &r);
    l--;
    int ex = r / ((int)400);
    if (all_same[ex] == 1)
      for (int i = ex * ((int)400); i < min(n, (ex + 1) * ((int)400)); i++)
        b[i] = 0;
    all_same[ex] = 0;
    ex = l / ((int)400);
    if (all_same[ex] == 1)
      for (int i = ex * ((int)400); i < min(n, (ex + 1) * ((int)400)); i++)
        b[i] = 0;
    all_same[ex] = 0;
    while (l % ((int)400) && l < r)
      ans +=
          min((long long)m[l], b[l] + 1LL * a[l] * (t - t_lst[l / ((int)400)])),
          b[l] = -1LL * a[l] * (t - t_lst[l / ((int)400)]), l++;
    ex = r / ((int)400);
    while (r % ((int)400) && l < r)
      r--,
          ans +=
          min((long long)m[r], b[r] + 1LL * a[r] * (t - t_lst[r / ((int)400)])),
          b[r] = -1LL * a[r] * (t - t_lst[r / ((int)400)]);
    for (int i = l / ((int)400); i < r / ((int)400); i++) {
      int lft = i * ((int)400), rght = min(n, (i + 1) * ((int)400));
      if (all_same[i]) {
        ans += 1LL * prt_a[rght - 1] * (t - t_lst[i]);
        pair<long long, int> pp;
        pp = {t - t_lst[i], ((int)101 * 1000)};
        int id = upper_bound(arr + lft, arr + rght, pp) - (arr + lft) - 1;
        if (id >= 0) {
          ans -= 1LL * prt_a[lft + id] * (t - t_lst[i]);
          ans += prt_m[lft + id];
        }
      } else {
        for (int j = lft; j < rght; j++)
          ans += min((long long)m[j], b[j] + 1LL * a[j] * (t - t_lst[i]));
      }
      t_lst[i] = t;
      all_same[i] = 1;
    }
    printf("%lld\n", ans);
  }
  return 0;
}