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>
#pragma warning(disable : 4996)
using namespace std;
int n, m;
long long a[300000][3];
struct NODE {
  int lazy;
  long long last;
  vector<int> pony;
  vector<long long> msum;
  vector<long long> rsum;
};
NODE itree[300000];
int S = 131072;
int gugan[300000][2];
bool ponycmp(int x, int y) { return a[x][2] * a[y][1] < a[x][1] * a[y][2]; }
void init(int now, int l, int r) {
  int i;
  gugan[now][0] = l, gugan[now][1] = r;
  if (now >= S) {
    if (now >= S + n) return;
    itree[now].lazy = 1;
    itree[now].pony.push_back(l);
    itree[now].msum.push_back(a[l][1]);
    itree[now].rsum.push_back(a[l][2]);
    return;
  }
  init(now * 2, l, (l + r) / 2);
  init(now * 2 + 1, (l + r) / 2 + 1, r);
  itree[now].pony.insert(itree[now].pony.end(), itree[now * 2].pony.begin(),
                         itree[now * 2].pony.end());
  itree[now].pony.insert(itree[now].pony.end(), itree[now * 2 + 1].pony.begin(),
                         itree[now * 2 + 1].pony.end());
  sort(itree[now].pony.begin(), itree[now].pony.end(), ponycmp);
  long long ms = 0, rs = 0;
  for (i = 0; i < itree[now].pony.size(); i++) {
    rs += a[itree[now].pony[i]][2];
    itree[now].msum.push_back(0);
    itree[now].rsum.push_back(rs);
  }
  for (i = itree[now].pony.size() - 1; i >= 0; i--) {
    ms += a[itree[now].pony[i]][1];
    itree[now].msum[i] = ms;
  }
}
long long getsum(int now, long long t, int s, int e) {
  if (s > e) return 0;
  if (gugan[now][0] == s && gugan[now][1] == e && itree[now].lazy == 1) {
    if (itree[now].last == 0)
      return min(a[now - S][0] + a[now - S][2] * t, a[now - S][1]);
    int low = 0, high = itree[now].pony.size() - 1, mid;
    long long dif = t - itree[now].last;
    while (low <= high) {
      mid = (low + high) / 2;
      if (dif * a[itree[now].pony[mid]][2] > a[itree[now].pony[mid]][1])
        high = mid - 1;
      else
        low = mid + 1;
    }
    long long dab1 = 0, dab2 = 0;
    if (low > 0) dab1 = itree[now].rsum[low - 1] * dif;
    if (low <= itree[now].msum.size() - 1) dab2 = itree[now].msum[low];
    return dab1 + dab2;
  }
  if (itree[now].lazy) itree[now * 2].lazy = itree[now * 2 + 1].lazy = 1;
  itree[now * 2].last = max(itree[now * 2].last, itree[now].last);
  itree[now * 2 + 1].last = max(itree[now * 2 + 1].last, itree[now].last);
  itree[now].last = -1;
  itree[now].lazy = 0;
  return getsum(now * 2, t, s, min(e, gugan[now * 2][1])) +
         getsum(now * 2 + 1, t, max(s, gugan[now * 2 + 1][0]), e);
}
void update(int now, long long t, int s, int e) {
  if (s > e) return;
  if (gugan[now][0] == s && gugan[now][1] == e) {
    itree[now].lazy = 1;
    itree[now].last = t;
    return;
  }
  if (itree[now].lazy) itree[now * 2].lazy = itree[now * 2 + 1].lazy = 1;
  itree[now * 2].last = max(itree[now * 2].last, itree[now].last);
  itree[now * 2 + 1].last = max(itree[now * 2 + 1].last, itree[now].last);
  itree[now].last = -1;
  itree[now].lazy = 0;
  update(now * 2, t, s, min(e, gugan[now * 2][1]));
  update(now * 2 + 1, t, max(s, gugan[now * 2 + 1][0]), e);
}
int main() {
  int i, j, k;
  scanf("%d", &n);
  for (i = 0; i < n; i++)
    for (j = 0; j < 3; j++) scanf("%I64d", &a[i][j]);
  for (i = 0; i < n; i++) {
    if (!a[i][1] && !a[i][2]) a[i][1] = 1;
  }
  init(1, 0, S - 1);
  scanf("%d", &m);
  for (i = 0; i < m; i++) {
    long long x, y, z;
    scanf("%I64d%I64d%I64d", &x, &y, &z);
    y--, z--;
    printf("%I64d\n", getsum(1, x, y, z));
    update(1, x, y, z);
  }
  return 0;
}