#include <bits/stdc++.h>   using namespace std;   inline void read(int &x) {     x = 0;     int f = 1;     char ch = getchar();     while (ch <  0  || ch >  9 ) {       if (ch ==  - ) f = -1;       ch = getchar();     }     while (ch >=  0  && ch <=  9 ) {       x = x * 10 + ch -  0 ;       ch = getchar();     }     x *= f;   }   inline void judge() {     freopen( in.txt ,  r , stdin);     freopen( out.txt ,  w , stdout);   }   const int maxn = 200005;   int n, Q, s[maxn], m[maxn], t[maxn], l[maxn], r[maxn], R[maxn];   set<pair<pair<int, int>, int> > res;   namespace seg {   long long suma[maxn << 5], sumb[maxn << 5];   int ls[maxn << 5], rs[maxn << 5], tot;   inline void update(int x) {     suma[x] = suma[ls[x]] + suma[rs[x]];     sumb[x] = sumb[ls[x]] + sumb[rs[x]];   }   inline void add(int x, int &y, int place, int va, int vb, int l, int r) {     y = ++tot;     suma[y] = suma[x];     sumb[y] = sumb[x];     ls[y] = ls[x];     rs[y] = rs[x];     suma[y] += va;     sumb[y] += vb;     if (l == r) {       return;     }     int md = (l + r) >> 1;     if (place <= md)       add(ls[x], ls[y], place, va, vb, l, md);     else       add(rs[x], rs[y], place, va, vb, md + 1, r);   }   inline long long queryA(int x, int l, int r, int lq, int rq) {     if (!x) return 0;     if (lq <= l && r <= rq) return suma[x];     long long res = 0;     int md = (l + r) >> 1;     if (lq <= md) res += queryA(ls[x], l, md, lq, rq);     if (rq > md) res += queryA(rs[x], md + 1, r, lq, rq);     return res;   }   inline long long queryB(int x, int l, int r, int lq, int rq) {     if (!x) return 0;     if (lq <= l && r <= rq) return sumb[x];     long long res = 0;     int md = (l + r) >> 1;     if (lq <= md) res += queryB(ls[x], l, md, lq, rq);     if (rq > md) res += queryB(rs[x], md + 1, r, lq, rq);     return res;   }   }  // namespace seg   int qtot = 0;   int root[maxn];   inline long long query(int l, int r, int tme) {     tme = min(tme, 99999);     return seg::queryA(root[r], 0, 100001, 0, tme) -            seg::queryA(root[l - 1], 0, 100001, 0, tme) +            1ll *                (seg::queryB(root[r], 0, 100001, tme + 1, 100001) -                 seg::queryB(root[l - 1], 0, 100001, tme + 1, 100001)) *                tme;   }   int main() {     read(n);     for (int i = (1); i <= (n); i++) read(s[i]), read(m[i]), read(R[i]);     for (int i = (1); i <= (n); i++) res.insert(make_pair(make_pair(i, i), -1));     read(Q);     for (int i = (1); i <= (Q); i++) read(t[i]), read(l[i]), read(r[i]);     for (int i = (1); i <= (n); i++) {       if (R[i] == 0) {         seg::add(root[i - 1], root[i], 100001, 0, R[i], 0, 100001);         continue;       }       int t = floor((long double)m[i] / R[i] + 0.99999999);       assert(1ll * t * R[i] >= m[i]);       assert(1ll * (t - 1) * R[i] < m[i]);       seg::add(root[i - 1], root[i], t, m[i], R[i], 0, 100001);     }     for (int i = (1); i <= (Q); i++) {       long long ans = 0;       set<pair<pair<int, int>, int> >::iterator it =           res.upper_bound(make_pair(make_pair(l[i], n + 1), 1));       vector<set<pair<pair<int, int>, int> >::iterator> rubi;       rubi.clear();       it--;       if (it->first.first < l[i]) {         res.insert(make_pair(make_pair(it->first.first, l[i] - 1), it->second));       }       int tmp1 = it->first.second, tmp2 = it->second;       res.erase(it);       res.insert(make_pair(make_pair(l[i], tmp1), tmp2));       it = res.upper_bound(make_pair(make_pair(l[i], n + 1), 1));       it--;       for (;;) {         if (it->second == -1) {           assert(it->first.first == it->first.second);           ans += min(               (long long)m[it->first.first],               (long long)s[it->first.first] + 1ll * R[it->first.first] * t[i]);         } else {           ans += query(it->first.first, min(it->first.second, r[i]),                        t[i] - it->second);           long long res = 0;         }         rubi.push_back(it);         if (it->first.second > r[i]) {           res.insert(               make_pair(make_pair(r[i] + 1, it->first.second), it->second));           break;         }         if (it->first.second == r[i]) break;         it++;       }       for (set<pair<pair<int, int>, int> >::iterator x : rubi) res.erase(x);       res.insert(make_pair(make_pair(l[i], r[i]), t[i]));       cout << ans << endl;     }     return 0;   }