453_E. Little Pony and Lord Tirek

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.

Example input:
5
0 10 1
0 12 1
0 20 1
0 12 1
0 10 1
2
5 1 5
19 1 5


Example output:
25
58


Tags: data structures
Difficulty rating: 3100

Solution:
#include <bits/stdc++.h>
using namespace std;
const int mxN = 1e5, bs = 275;
int n, q, s[mxN], m[mxN], r[mxN], g[(mxN - 1) / bs + 1][mxN + 3],
    lt1[(mxN - 1) / bs + 1], lt2[mxN], ti, li, ri;
int main() {
  ios_base::sync_with_stdio(0);
  cin.tie(0);
  cin >> n;
  for (int i = 0; i < n; ++i) {
    cin >> s[i] >> m[i] >> r[i];
    if (!r[i]) continue;
    g[i / bs][1] += r[i];
    g[i / bs][m[i] / r[i] + 1] += m[i] % r[i] - r[i];
    g[i / bs][m[i] / r[i] + 2] -= m[i] % r[i];
  }
  for (int i = 0; i <= (n - 1) / bs; ++i) {
    lt1[i] = -1;
    for (int j = 2; j <= mxN; ++j) g[i][j] += g[i][j - 1];
    for (int j = 2; j <= mxN; ++j) g[i][j] += g[i][j - 1];
  }
  cin >> q;
  while (q--) {
    cin >> ti >> li >> ri, --li, --ri;
    long long ans = 0;
    auto psh = [&](int i) {
      if (lt1[i] == -1) return;
      for (int j = i * bs; j < min((i + 1) * bs, n); ++j) lt2[j] = lt1[i];
      lt1[i] = -1;
    };
    psh(li / bs);
    psh(ri / bs);
    auto ue = [&](int i) {
      ans += min(s[i] + 1ll * (ti - lt2[i]) * r[i], 1ll * m[i]);
      s[i] = 0;
      lt2[i] = ti;
    };
    for (; li % bs && li <= ri; ++li) ue(li);
    for (; li + bs <= ri; li += bs) {
      if (lt1[li / bs] == -1)
        for (int j = li; j < li + bs; ++j) ue(j);
      else
        ans += g[li / bs][min(ti - lt1[li / bs], mxN)];
      lt1[li / bs] = ti;
    }
    for (; li <= ri; ++li) ue(li);
    cout << ans << "\n";
  }
}