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;
inline char gc() {
  static char now[1 << 16], *S, *T;
  if (T == S) {
    T = (S = now) + fread(now, 1, 1 << 16, stdin);
    if (T == S) return EOF;
  }
  return *S++;
}
inline int read() {
  int x = 0, f = 1;
  char ch = gc();
  while (!isdigit(ch)) {
    if (ch == '-') f = -1;
    ch = gc();
  }
  while (isdigit(ch)) x = x * 10 + ch - '0', ch = gc();
  return x * f;
}
const int N = 1e5 + 10;
const int inf = 0x3f3f3f3f;
struct node1 {
  int s, m, r, t;
} a[N];
struct node {
  vector<long long> pre, suc;
  vector<node1> f;
  vector<int> tim;
  int tag;
  inline void merge(const vector<node1> &a, const vector<node1> &b) {
    int n1 = a.size(), n2 = b.size();
    int now1 = 0, now2 = 0;
    while (now1 < n1 && now2 < n2) {
      if (a[now1].t < b[now2].t)
        f.push_back(a[now1]), ++now1;
      else
        f.push_back(b[now2]), ++now2;
    }
    while (now1 < n1) f.push_back(a[now1]), ++now1;
    while (now2 < n2) f.push_back(b[now2]), ++now2;
    long long pre1 = 0, suc1 = 0;
    for (int i = 0; i < f.size(); ++i)
      pre1 += f[i].m, pre.push_back(pre1), tim.push_back(f[i].t);
    suc.resize(f.size() + 10);
    for (int i = f.size() - 1; ~i; --i) suc1 += f[i].r, suc[i] = suc1;
  }
  inline long long gao(int t) {
    t = t - tag;
    int p = lower_bound(tim.begin(), tim.end(), t) - tim.begin();
    long long tmp = 0;
    if (p) tmp += pre[p - 1];
    if (p != tim.size()) tmp += (long long)suc[p] * t;
    return tmp;
  }
} tree[N << 2];
int n, t, m;
inline void update(int x) {
  if (tree[(x << 1)].tag == -2 || tree[(x << 1 | 1)].tag == -2) {
    tree[x].tag = -2;
    return;
  }
  if (tree[(x << 1)].tag == tree[(x << 1 | 1)].tag && tree[(x << 1)].tag >= 0) {
    tree[x].tag = tree[(x << 1)].tag;
    return;
  }
  tree[x].tag = -1;
}
inline void build(int x, int l, int r) {
  tree[x].tag = -2;
  if (l == r) {
    tree[x].f.push_back(a[l]);
    tree[x].pre.push_back(a[l].m);
    tree[x].tim.push_back(a[l].t);
    tree[x].suc.push_back(a[l].r);
    return;
  }
  int mid = l + r >> 1;
  build((x << 1), l, mid);
  build((x << 1 | 1), mid + 1, r);
  tree[x].merge(tree[(x << 1)].f, tree[(x << 1 | 1)].f);
}
inline void pushdown(int x) {
  if (tree[x].tag >= 0)
    tree[(x << 1)].tag = tree[(x << 1 | 1)].tag = tree[x].tag;
}
inline long long query(int x, int l, int r, int l1, int r1) {
  if (l == r && tree[x].tag == -2) {
    long long tmp = a[l].s;
    tmp += (long long)t * a[l].r;
    tmp = min(tmp, (long long)a[l].m);
    tree[x].tag = t;
    return tmp;
  }
  if (l1 <= l && r1 >= r && tree[x].tag >= 0) {
    long long tmp = tree[x].gao(t);
    tree[x].tag = t;
    return tmp;
  }
  int mid = l + r >> 1;
  pushdown(x);
  long long tmp = 0;
  if (l1 <= mid) tmp += query((x << 1), l, mid, l1, r1);
  if (r1 > mid) tmp += query((x << 1 | 1), mid + 1, r, l1, r1);
  update(x);
  return tmp;
}
int main() {
  n = read();
  for (int i = 1; i <= n; ++i) {
    a[i].s = read(), a[i].m = read(), a[i].r = read();
    a[i].t = a[i].r ? a[i].m / a[i].r : inf;
  }
  build(1, 1, n);
  m = read();
  for (int i = 1; i <= m; ++i) {
    int l, r;
    t = read();
    l = read();
    r = read();
    printf("%lld\n", query(1, 1, n, l, r));
  }
  return 0;
}