#include <bits/stdc++.h>   using namespace std;   struct data {     int s, m, r, t;     int operator<(const data &b) const { return t < b.t; }     void get() {       scanf( %d , &s);       scanf( %d , &m);       scanf( %d , &r);       t = r ? (m / r + (m % r > 0)) : 2e9;     }     int calc(int o) { return min(1ll * m, s + 1ll * r * o); }   } a[20][100005];   int T[100005 << 2];   long long R[20][100005], sum[20][100005], ans;   int n, Q, t, ql, qr;   void Build(int x, int l, int r, int d) {     T[x] = -2;     if (l == r) {       a[d][l].get();       R[d][l] = R[d][l - 1] + a[d][l].r;       sum[d][l] = sum[d][l - 1] + a[d][l].m;       return;     }     int mid = (l + r) >> 1;     Build(x << 1, l, mid, d + 1);     Build(x << 1 | 1, mid + 1, r, d + 1);     for (int i = l; i <= r; i++) a[d][i] = a[d + 1][i];     sort(a[d] + l, a[d] + r + 1);     for (int i = l; i <= r; i++) R[d][i] = R[d][i - 1] + a[d][i].r;     for (int i = l; i <= r; i++) sum[d][i] = sum[d][i - 1] + a[d][i].m;   }   void Find(int x, int l, int r, int d) {     if (ql <= l && r <= qr && T[x] != -1) {       if (T[x] == -2) {         for (int i = l; i <= r; i++) ans += a[d][i].calc(t);         T[x] = t;         return;       }       data tmp = (data){0, 0, 0, t - T[x]};       int id = upper_bound(a[d] + l, a[d] + r + 1, tmp) - a[d];       ans += 1ll * (R[d][r] - R[d][id - 1]) * (t - T[x]) + sum[d][id - 1] -              sum[d][l - 1];       T[x] = t;       return;     }     if (T[x] >= 0) T[x << 1] = T[x << 1 | 1] = T[x];     int mid = (l + r) >> 1;     if (ql <= mid) Find(x << 1, l, mid, d + 1);     if (qr > mid) Find(x << 1 | 1, mid + 1, r, d + 1);     T[x] = (T[x << 1] == T[x << 1 | 1]) ? T[x << 1] : -1;   }   int main() {     scanf( %d , &n);     Build(1, 1, n, 0);     scanf( %d , &Q);     while (Q--) {       scanf( %d , &t);       scanf( %d , &ql);       scanf( %d , &qr);       ans = 0;       Find(1, 1, n, 0);       printf( %I64d n , ans);     }   }   