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>
template <typename T>
void read(T &x);
template <typename T>
void read(T &x, T &y);
template <typename T>
void read(T &x, T &y, T &z);
template <typename T>
void write(T x);
template <typename T>
void writeC(T x);
template <typename T>
void writeN(T x);
const int lim = 1e5 + 2;
int n, s[100005], m[100005], rg[100005], q;
long long ans[100005];
struct LordTirek {
  long long sum[100005];
  int magic(int x) { return x & -x; }
  void add(int x, int y) {
    while (x <= lim) {
      sum[x] += y;
      x += magic(x);
    }
  }
  long long query(int x) {
    long long ans = 0;
    while (x > 0) {
      ans += sum[x];
      x -= magic(x);
    }
    return ans;
  }
} A, B;
struct data {
  int x, det, id;
};
namespace scanner {
std::vector<data> qry[100005];
void Add(int l, int r, int x, int id) {
  x = std::min(x, lim);
  qry[r].push_back(data({x, 1, id}));
  qry[l - 1].push_back(data({x, -1, id}));
}
void work() {
  for (int i = 1; i <= n; ++i) {
    if (rg[i]) {
      B.add(m[i] / rg[i] + 1, rg[i]);
      A.add(m[i] / rg[i] + 1, m[i]);
    }
    for (auto x : qry[i]) {
      long long val = A.query(x.x) + (B.query(lim) - B.query(x.x)) * x.x;
      ans[x.id] += x.det * val;
    }
  }
}
}  // namespace scanner
std::set<std::pair<std::pair<int, int>, int> > S;
void pushall(int l, int r, int x, int i) {
  std::stack<std::pair<std::pair<int, int>, int> > inc, stk;
  auto p =
      std::prev(S.upper_bound(std::make_pair(std::make_pair(l, 1e9), 1e9)));
  while (1) {
    if (p == S.end()) break;
    int L = (*p).first.first, R = (*p).first.second, w = (*p).second;
    if (L > r) break;
    int l1 = std::max(L, l), r1 = std::min(R, r);
    if (l1 <= r1) {
      if (w) {
        scanner::Add(l1, r1, x - w, i);
      } else
        ans[i] += std::min((long long)x * rg[l1] + s[l1], (long long)m[l1]);
    }
    stk.push(*p);
    if (L < l) inc.push(std::make_pair(std::make_pair(L, l - 1), w));
    if (r < R) inc.push(std::make_pair(std::make_pair(r + 1, R), w));
    p++;
  }
  while (!stk.empty()) {
    S.erase(stk.top());
    stk.pop();
  }
  while (!inc.empty()) {
    S.insert(inc.top());
    inc.pop();
  }
  S.insert(std::make_pair(std::make_pair(l, r), x));
}
signed main() {
  read(n);
  for (int i = (1); i <= (n); ++i) {
    read(s[i], m[i], rg[i]);
  }
  for (int i = (1); i <= (n); ++i)
    S.insert(std::make_pair(std::make_pair(i, i), 0));
  S.insert(std::make_pair(std::make_pair(0, 0), 0));
  S.insert(std::make_pair(std::make_pair(n + 1, n + 1), 0));
  read(q);
  for (int i = (1); i <= (q); ++i) {
    int l, r, t;
    read(t, l, r);
    pushall(l, r, t, i);
  }
  scanner::work();
  for (int i = 1; i <= q; ++i) printf("%lld\n", ans[i]);
  return 0;
}
template <typename T>
void read(T &x) {
  x = 0;
  char c = getchar();
  T flag = 1;
  while (c < '0' || c > '9') {
    if (c == '-') flag = -flag;
    c = getchar();
  }
  while ('0' <= c && c <= '9') {
    x = x * 10 + c - '0';
    c = getchar();
  }
  x *= flag;
}
template <typename T>
void read(T &x, T &y) {
  read(x);
  read(y);
}
template <typename T>
void read(T &x, T &y, T &z) {
  read(x);
  read(y);
  read(z);
}
template <typename T>
void writeC(T x) {
  if (x < 0) x = ~x + 1, putchar('-');
  if (x > 9) writeC(x / 10);
  putchar(x % 10 + '0');
}
template <typename T>
void write(T x) {
  writeC(x);
  putchar(' ');
}
template <typename T>
void writeN(T x) {
  writeC(x);
  puts("");
}