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 inf = int(1e9);
const double pi = 4 * atan(double(1.0));
const double eps = 1e-9;
const int N = int(1e5) + 100;
struct ponny {
  int s, m, r;
};
struct node {
  int t;
  long long sum, add_a, add_b;
  int h, sz, p, l, r;
};
int root = 0;
set<pair<int, int> > times;
ponny p[N];
int t[N];
node d[N];
vector<int> add[N], del[N];
inline int get_rand() { return (rand() << 16) | rand(); }
inline void push(int v) {
  if (v == 0) {
    return;
  }
  d[v].sum += 1LL * d[v].add_a * d[v].t + d[v].add_b;
  if (d[v].l > 0) {
    d[d[v].l].add_a += d[v].add_a;
    d[d[v].l].add_b += d[v].add_b;
  }
  if (d[v].r > 0) {
    d[d[v].r].add_a += d[v].add_a;
    d[d[v].r].add_b += d[v].add_b;
  }
  d[v].add_a = d[v].add_b = 0;
}
inline void calc(int v) {
  d[v].p = 0;
  d[v].sz = d[d[v].l].sz + d[d[v].r].sz + 1;
  if (d[v].l > 0) {
    d[d[v].l].p = v;
  }
  if (d[v].r > 0) {
    d[d[v].r].p = v;
  }
}
void split(int v, int t, int &l, int &r) {
  push(v);
  if (v == 0) {
    l = r = 0;
    return;
  }
  if (d[v].t < t) {
    split(d[v].r, t, d[v].r, r);
    l = v;
  } else {
    split(d[v].l, t, l, d[v].l);
    r = v;
  }
  calc(v);
}
void split_size(int v, int sz, int &l, int &r) {
  push(v);
  if (v == 0) {
    l = r = 0;
    return;
  }
  if (d[d[v].l].sz + 1 <= sz) {
    split_size(d[v].r, sz - d[d[v].l].sz - 1, d[v].r, r);
    l = v;
  } else {
    split_size(d[v].l, sz, l, d[v].l);
    r = v;
  }
  calc(v);
}
int merge(int l, int r) {
  push(l);
  push(r);
  if (l == 0) {
    return r;
  }
  if (r == 0) {
    return l;
  }
  int res;
  if (d[l].h > d[r].h) {
    d[l].r = merge(d[l].r, r);
    res = l;
  } else {
    d[r].l = merge(l, d[r].l);
    res = r;
  }
  calc(res);
  return res;
}
inline void insert_num(int num) {
  int l, r;
  split(root, d[num].t, l, r);
  root = merge(l, merge(num, r));
}
inline int get_num(int v) {
  int res = d[d[v].l].sz + 1;
  while (d[v].p > 0) {
    int pv = d[v].p;
    if (d[pv].r == v) {
      res += d[d[pv].l].sz + 1;
    }
    v = pv;
  }
  return res;
}
inline void del_num(int num) {
  int n = get_num(num);
  int l, r, tmp;
  split_size(root, n, l, r);
  split_size(l, n - 1, l, tmp);
  assert(tmp == num);
  root = merge(l, r);
}
inline void insert_with_new_diff(int num, int new_diff) {
  if (times.find(make_pair(t[num], num)) != times.begin()) {
    del_num(num);
  }
  d[num].t = new_diff;
  insert_num(num);
}
inline void add_query(int num) {
  set<pair<int, int> >::iterator it = times.upper_bound(make_pair(t[num], num));
  d[num].h = get_rand();
  d[num].sz = 1;
  d[num].add_a = d[num].add_b = d[num].sum = 0;
  d[num].p = d[num].l = d[num].r = 0;
  if (it != times.end()) {
    int next_num = it->second;
    insert_with_new_diff(next_num, it->first - t[num]);
  }
  times.insert(make_pair(t[num], num));
  it = times.find(make_pair(t[num], num));
  if (it == times.begin()) {
    return;
  }
  --it;
  d[num].t = t[num] - it->first;
  insert_num(num);
}
inline void del_query(int num) {
  set<pair<int, int> >::iterator it = times.find(make_pair(t[num], num));
  int add = 0;
  if (it != times.begin()) {
    del_num(num);
    add = d[num].t;
  }
  ++it;
  if (it != times.end()) {
    int next_num = it->second;
    if (add > 0) {
      insert_with_new_diff(next_num, d[next_num].t + add);
    } else {
      del_num(next_num);
    }
  }
  times.erase(make_pair(t[num], num));
}
int main() {
  int n;
  scanf("%d", &n);
  for (int i = 0; i < n; ++i) {
    scanf("%d %d %d", &p[i].s, &p[i].m, &p[i].r);
  }
  int m;
  scanf("%d", &m);
  for (int i = 1; i <= m; ++i) {
    int l, r;
    scanf("%d %d %d", &t[i], &l, &r);
    --l;
    --r;
    add[l].push_back(i);
    del[r].push_back(i);
  }
  for (int i = 0; i < n; ++i) {
    for (int j = 0; j < int((add[i]).size()); ++j) {
      add_query(add[i][j]);
    }
    if (!times.empty()) {
      int first = times.begin()->second;
      long long mana = min(1LL * t[first] * p[i].r + p[i].s, 1LL * p[i].m);
      d[first].sum += mana;
      if (times.size() >= 2 && p[i].r > 0) {
        int critical = (p[i].m + p[i].r - 1) / p[i].r;
        int l, r;
        split(root, critical, l, r);
        if (r > 0) {
          d[r].add_b += p[i].m;
        }
        if (l > 0) {
          d[l].add_a += p[i].r;
        }
        root = merge(l, r);
      }
    }
    for (int j = 0; j < int((del[i]).size()); ++j) {
      del_query(del[i][j]);
    }
  }
  for (int i = 1; i <= m; ++i) {
    printf(
        "%lld"
        "\n",
        d[i].sum);
  }
  return 0;
}