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 = 1000000001;
const int maxn = 100100;
struct node {
 public:
  int s, m, r, fin;
  node(int x = 0) { fin = x; }
  void sin() {
    cin >> s >> m >> r;
    fin = r ? (m / r + (m % r > 0)) : inf;
  }
  long long int day(long long int uu) {
    return min((long long int)s + (long long int)r * uu, (long long int)m);
  }
} arr[18][maxn];
bool operator<(node a, node b) { return a.fin < b.fin; }
int blok[maxn * 4];
long long int prem[18][maxn * 4], prer[18][maxn * 4];
void build(int nod, int l, int r, int d) {
  blok[nod] = -2;
  if (l == r) {
    arr[d][l].sin();
    prem[d][l] = prem[d][l - 1] + arr[d][l].m;
    prer[d][l] = prer[d][l - 1] + arr[d][l].r;
    return;
  }
  int mid = (l + r) >> 1, i;
  build(nod << 1, l, mid, d + 1);
  build(nod << 1 | 1, mid + 1, r, d + 1);
  for (i = l; i <= r; i++) arr[d][i] = arr[d + 1][i];
  sort(arr[d] + l, arr[d] + r + 1);
  for (i = l; i <= r; i++) {
    prem[d][i] = prem[d][i - 1] + arr[d][i].m;
    prer[d][i] = prer[d][i - 1] + arr[d][i].r;
  }
}
void pushdown(int x) {
  if (blok[x] >= 0) blok[x << 1] = blok[x << 1 | 1] = blok[x];
}
void pushup(int x) {
  blok[x] = blok[x << 1];
  if (blok[x] != blok[x << 1 | 1]) blok[x] = -1;
}
long long int query(int nod, int a, int b, int l, int r, int T, int d) {
  int i;
  long long int res = 0;
  if (l <= a && b <= r && blok[nod] != -1) {
    if (blok[nod] == -2) {
      for (i = a; i <= b; i++) res += arr[d][i].day(T);
    } else {
      node ab;
      ab.fin = T - blok[nod];
      ab.m = inf;
      ab.r = inf;
      ab.s = inf;
      i = upper_bound(arr[d] + a, arr[d] + b + 1, ab) - arr[d] - 1;
      res = (T - blok[nod]) * (prer[d][b] - prer[d][i]) + prem[d][i] -
            prem[d][a - 1];
    }
    blok[nod] = T;
    return res;
  }
  pushdown(nod);
  i = (a + b) >> 1;
  if (l <= i) res += query(nod << 1, a, i, l, r, T, d + 1);
  if (i < r) res += query(nod << 1 | 1, i + 1, b, l, r, T, d + 1);
  pushup(nod);
  return res;
}
int main() {
  int n;
  cin >> n;
  build(1, 1, n, 0);
  int m;
  cin >> m;
  while (m--) {
    long long int t, l, r;
    scanf("%lld%lld%lld", &t, &l, &r);
    long long int ans = 0;
    ans = query(1, 1, n, l, r, t, 0);
    printf("%lld\n", ans);
  }
  return 0;
}