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 MAX = (1 << 17);
const int INF = 1000000005;
class SegTree {
 public:
  long long seg[2 * MAX];
  void init() { memset(seg, 0, sizeof(seg)); }
  void insert(int v, int i) {
    for (i += MAX; i > 0; i /= 2) seg[i] += v;
  }
  long long lookup2(int l, int r, int p, int a, int b) {
    if (a >= r || b <= l) return 0;
    if (l <= a && b <= r) return seg[p];
    int m = (a + b) / 2;
    return lookup2(l, r, p * 2, a, m) + lookup2(l, r, p * 2 + 1, m, b);
  }
  long long lookup(int l, int r) {
    r++;
    return lookup2(l, r, 1, 0, MAX);
  }
};
struct pony {
  long long s, m, r;
};
int N;
pony P[MAX];
pair<int, int> time_maxed[MAX];
SegTree maxed;
SegTree rate;
struct query {
  int l, r, t;
  int i;
  long long v;
};
bool query_cmp(query a, query b) { return a.t < b.t; }
vector<query> Q;
void process_queries() {
  for (int i = 1; i <= N; i++)
    time_maxed[i] =
        make_pair(P[i].r == 0 ? INF : (P[i].m + P[i].r - 1) / P[i].r, i);
  sort(time_maxed + 1, time_maxed + 1 + N);
  int cur_time_i = 1;
  maxed.init();
  rate.init();
  for (int i = 1; i <= N; i++) rate.insert(P[i].r, i);
  sort(Q.begin(), Q.end(), query_cmp);
  for (int qi = 0; qi < Q.size(); qi++) {
    query& q = Q[qi];
    if (q.t < 0) {
      assert(q.l == q.r);
      int i = q.l, t = q.t + INF;
      q.v = min(P[i].s + t * P[i].r, P[i].m);
      continue;
    }
    while (cur_time_i <= N && q.t >= time_maxed[cur_time_i].first) {
      int i = time_maxed[cur_time_i].second;
      maxed.insert(P[i].m, i);
      rate.insert(-P[i].r, i);
      cur_time_i++;
    }
    q.v = maxed.lookup(q.l, q.r) + q.t * rate.lookup(q.l, q.r);
  }
}
map<pair<int, char>, int> segments;
long long res[MAX];
int main() {
  ios::sync_with_stdio(false);
  cin >> N;
  for (int i = 1; i <= N; i++) cin >> P[i].s >> P[i].m >> P[i].r;
  for (int i = 1; i <= N; i++) {
    segments[make_pair(i, 'l')] = INF;
    segments[make_pair(i, 'r')] = INF;
  }
  int M;
  cin >> M;
  for (int m = 0; m < M; m++) {
    int t, l, r;
    cin >> t >> l >> r;
    map<pair<int, char>, int>::iterator it;
    it = segments.lower_bound(make_pair(l, 'l'));
    if (it->first.second == 'r') {
      segments[make_pair(l - 1, 'r')] = it->second;
      segments[make_pair(l, 'l')] = it->second;
    }
    it = --segments.upper_bound(make_pair(r, 'r'));
    if (it->first.second == 'l') {
      segments[make_pair(r + 1, 'l')] = it->second;
      segments[make_pair(r, 'r')] = it->second;
    }
    for (it = segments.lower_bound(make_pair(l, 'l'));
         it != segments.upper_bound(make_pair(r, 'r'));) {
      int prev_t = it->second;
      assert(it->first.second == 'l');
      int prev_l = it->first.first;
      it++;
      assert(it->first.second == 'r');
      int prev_r = it->first.first;
      it++;
      query q;
      q.l = prev_l, q.r = prev_r, q.t = t - prev_t, q.i = m;
      Q.push_back(q);
      segments.erase(make_pair(prev_l, 'l'));
      segments.erase(make_pair(prev_r, 'r'));
    }
    segments[make_pair(l, 'l')] = t;
    segments[make_pair(r, 'r')] = t;
  }
  process_queries();
  memset(res, 0, sizeof(res));
  for (int i = 0; i < Q.size(); i++) res[Q[i].i] += Q[i].v;
  for (int i = 0; i < M; i++) cout << res[i] << '\n';
}