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;
template <class T>
void debug(T a, T b) {
  ;
}
template <class T>
void chmin(T& a, const T& b) {
  if (a > b) a = b;
}
template <class T>
void chmax(T& a, const T& b) {
  if (a < b) a = b;
}
namespace std {
template <class S, class T>
ostream& operator<<(ostream& out, const pair<S, T>& a) {
  out << '(' << a.first << ',' << a.second << ')';
  return out;
}
}  // namespace std
const int INF1 = 1e9 + 5;
const long long int INF2 = 1e18;
int n, m;
long long int st[100005], mx[100005], incr[100005];
struct segtree {
  int start[400005];
  vector<pair<pair<long long int, long long int>, long long int> > val[270000];
  int n;
  pair<pair<long long int, long long int>, long long int> make(int m,
                                                               int incr) {
    pair<pair<long long int, long long int>, long long int> res;
    if (incr == 0)
      res.first.first = INF1;
    else
      res.first.first = m / incr;
    res.first.second = m;
    res.second = incr;
    return res;
  }
  void mrg(int i) {
    if (start[i * 2 + 1] == start[i * 2 + 2])
      start[i] = start[i * 2 + 1];
    else if (start[i * 2 + 2] == -2)
      start[i] = start[i * 2 + 1];
    else
      start[i] = -1;
  }
  void push(int i) {
    if (start[i] >= 0) {
      start[i * 2 + 1] = start[i];
      start[i * 2 + 2] = start[i];
    }
  }
  void init(int n_) {
    n = 1;
    while (n < n_) n <<= 1;
    for (int i = 0; i < (n); ++i) start[i + n - 1] = -2;
    for (int i = 0; i < (n_); ++i) {
      start[i + n - 1] = 0;
      val[i + n - 1].push_back(make(mx[i], incr[i]));
    }
    for (int i = n - 2; i >= 0; --i) {
      mrg(i);
      val[i].resize(val[i * 2 + 1].size() + val[i * 2 + 2].size());
      merge((val[i * 2 + 1]).begin(), (val[i * 2 + 1]).end(),
            (val[i * 2 + 2]).begin(), (val[i * 2 + 2]).end(), val[i].begin());
    }
    for (int i = n - 2; i >= 0; --i)
      if (!val[i].empty()) {
        for (int j = 0; j < (val[i].size() - 1); ++j) {
          val[i][j + 1].first.second += val[i][j].first.second;
          val[i][j + 1].second += val[i][j].second;
        }
      }
  }
  long long int query(int a, int b, int i, int l, int r, int t) {
    if (a <= l && r <= b && (start[i] > 0 || r - l == 1)) {
      int dif = t - start[i];
      if (start[i] == 0) {
        start[i] = t;
        return min(mx[l], st[l] + incr[l] * t);
      }
      int lb =
          lower_bound((val[i]).begin(), (val[i]).end(),
                      (pair<pair<long long int, long long int>, long long int>)
                          make_pair(make_pair(dif, -1), -1)) -
          val[i].begin();
      long long int res = 0;
      if (lb > 0) {
        --lb;
        res += val[i][lb].first.second;
        res += (val[i].back().second - val[i][lb].second) * dif;
      } else {
        res += val[i].back().second * dif;
      }
      start[i] = t;
      return res;
    }
    if (b <= l || r <= a) return 0;
    int md = (l + r) >> 1;
    push(i);
    long long int lch = query(a, b, i * 2 + 1, l, md, t),
                  rch = query(a, b, i * 2 + 2, md, r, t);
    mrg(i);
    return lch + rch;
  }
};
segtree seg;
int main() {
  cin >> n;
  for (int i = 0; i < (n); ++i) {
    int a, b, c;
    scanf("%d%d%d", &a, &b, &c);
    st[i] = a;
    mx[i] = b;
    incr[i] = c;
  }
  seg.init(n);
  cin >> m;
  for (int hoge = 0; hoge < (m); ++hoge) {
    int t, l, r;
    scanf("%d%d%d", &t, &l, &r);
    --l;
    long long int res = seg.query(l, r, 0, 0, seg.n, t);
    cout << res << '\n';
  }
  return 0;
}