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>
const int maxn = 100100;
using std::cin;
using std::cout;
int n, m;
std::map<int, int> map;
inline std::map<int, int>::iterator split(int r) {
  auto iter = map.lower_bound(r);
  return iter->first == r ? iter
                          : map.emplace_hint(iter, r, prev(iter)->second);
}
int start[maxn], max[maxn], add[maxn];
struct ask {
  int l, r, t, id;
};
long long ans[maxn];
std::vector<ask> vec;
inline int cmp0(const ask& x, const ask& y) { return x.t < y.t; }
inline void calc0(int l, int r, int t, int id) {
  vec.push_back((ask){l, r, t, id});
}
inline void calc1(int l, int r, int t, int id) {
  for (int i = l; i <= r; ++i)
    ans[id] += std::min<long long>(max[i], start[i] + (long long)add[i] * t);
}
inline void make(int l, int r, int t, int id) {
  auto R = split(r + 1), L = split(l);
  for (auto i = L; i != R; ++i) {
    if (i->second != -1)
      calc0(i->first, next(i)->first - 1, t - i->second, id);
    else
      calc1(i->first, next(i)->first - 1, t, id);
  }
  map.erase(L, R), map[l] = t;
}
long long sum[maxn << 2], rem[maxn << 2], min_add[maxn << 2], tag[maxn << 2];
inline void update(int cur) {
  sum[cur] = sum[cur << 1] + sum[cur << 1 | 1];
  rem[cur] = rem[cur << 1] + rem[cur << 1 | 1];
  min_add[cur] = std::min(min_add[cur << 1], min_add[cur << 1 | 1]);
}
inline void build(int cur, int l, int r) {
  if (l == r) {
    rem[cur] = add[l];
    min_add[cur] = add[l] ? max[l] / add[l] : int(1e9);
    return;
  }
  int mid = l + r >> 1;
  build(cur << 1, l, mid);
  build(cur << 1 | 1, mid + 1, r);
  update(cur);
}
inline void put(int cur, long long v) {
  sum[cur] += rem[cur] * v;
  tag[cur] += v;
  min_add[cur] -= v;
}
inline void pushdown(int cur) {
  if (tag[cur] != 0) {
    put(cur << 1, tag[cur]);
    put(cur << 1 | 1, tag[cur]);
    tag[cur] = 0;
  }
}
inline void upt(int v, int cur = 1, int l = 1, int r = n) {
  if (v <= min_add[cur]) return put(cur, v);
  if (l == r) {
    sum[cur] = max[l];
    rem[cur] = 0;
    min_add[cur] = 1e9;
    return;
  }
  int mid = l + r >> 1;
  pushdown(cur);
  upt(v, cur << 1, l, mid);
  upt(v, cur << 1 | 1, mid + 1, r);
  update(cur);
}
inline long long ask(int ql, int qr, int cur = 1, int l = 1, int r = n) {
  if (ql <= l && r <= qr) return sum[cur];
  int mid = l + r >> 1;
  pushdown(cur);
  long long ans = 0;
  if (ql <= mid) ans += ask(ql, qr, cur << 1, l, mid);
  if (mid < qr) ans += ask(ql, qr, cur << 1 | 1, mid + 1, r);
  return ans;
}
int main() {
  std::ios::sync_with_stdio(false), cin.tie(0);
  cin >> n;
  for (int i = 1; i <= n; ++i) {
    cin >> start[i] >> max[i] >> add[i];
  }
  cin >> m;
  map[0] = map[n + 1] = -1;
  for (int i = 1, t, l, r; i <= m; ++i) {
    cin >> t >> l >> r;
    make(l, r, t, i);
  }
  sort(vec.begin(), vec.end(), cmp0);
  int last_time = 0;
  build(1, 1, n);
  for (const auto& x : vec) {
    if (x.t != last_time) {
      upt(x.t - last_time), last_time = x.t;
    }
    ans[x.id] += ask(x.l, x.r);
  }
  for (int i = 1; i <= m; ++i) {
    cout << ans[i] << '\n';
  }
}