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 N = 100100;
vector<int> vec[N * 4], id[N * 4];
vector<long long> s1[N * 4], s2[N * 4];
int a[N], b[N], c[N];
long long sum[N * 4];
int tag[N * 4];
void bt(int o, int l, int r) {
  tag[o] = -1;
  if (l == r) {
    vec[o].push_back(c[l] ? b[l] / c[l] : INT_MAX);
    id[o].push_back(l);
    s1[o].push_back(0);
    s2[o].push_back(0);
    s1[o].push_back(c[l]);
    s2[o].push_back(b[l]);
    sum[o] = c[l];
    return;
  }
  int mid = (l + r) / 2, L = mid - l + 1, R = r - mid;
  bt(o * 2, l, mid);
  bt(o * 2 + 1, mid + 1, r);
  s1[o].push_back(0);
  s2[o].push_back(0);
  for (int i = 0, j = 0; i < L || j < R;)
    if (i < L && (j == R || vec[o * 2][i] < vec[o * 2 + 1][j])) {
      vec[o].push_back(vec[o * 2][i]);
      id[o].push_back(id[o * 2][i]);
      ++i;
    } else {
      vec[o].push_back(vec[o * 2 + 1][j]);
      id[o].push_back(id[o * 2 + 1][j]);
      ++j;
    }
  for (int i : id[o])
    s1[o].push_back(s1[o].back() + c[i]), s2[o].push_back(s2[o].back() + b[i]);
  sum[o] = s1[o][L + R];
}
int q1, q2, q3;
long long q4;
void query(int o, int l, int r) {
  int mid = (l + r) / 2;
  if (q1 <= l && r <= q2) {
    if (tag[o] >= 0) {
      int d = lower_bound(vec[o].begin(), vec[o].end(), q3 - tag[o]) -
              vec[o].begin();
      q4 += (long long)(sum[o] - s1[o][d]) * (q3 - tag[o]) + s2[o][d];
    } else if (l == r)
      q4 += min((long long)b[l], a[l] + (long long)q3 * c[l]);
    else
      query(o * 2, l, mid), query(o * 2 + 1, mid + 1, r);
    tag[o] = q3;
    return;
  }
  if (tag[o] >= 0) {
    tag[o * 2] = tag[o * 2 + 1] = tag[o];
    tag[o] = -1;
  }
  if (q1 <= mid) query(o * 2, l, mid);
  if (q2 > mid) query(o * 2 + 1, mid + 1, r);
}
int n, m;
int main() {
  ios::sync_with_stdio(0);
  cin.tie(0);
  cout.tie(0);
  cin >> n;
  for (int i = 1; i <= n; ++i) cin >> a[i] >> b[i] >> c[i];
  bt(1, 1, n);
  for (cin >> m; m-- > 0;) {
    cin >> q3 >> q1 >> q2;
    q4 = 0;
    query(1, 1, n);
    cout << q4 << '\n';
  }
  return 0;
}