#include using namespace std; using ll = long long; const int SQ = 500; using tii = tuple; int main() { // 場所ごとに「減り始めるイベント」「減らなくなるイベント」が1回ずつある // 地獄のような平方分割 int N; cin >> N; vector A(N); vector T(N); for (int i = 0; i < N; i++) { cin >> A[i] >> T[i]; } int Q; cin >> Q; vector queries; for (int i = 0; i < Q; i++) { int d, l, r; cin >> d >> l >> r; l--; r--; queries.push_back({d, l, r}); } vector evt; for (int i = 0; i < N; i++) { int t = T[i]; evt.push_back({t, -1, i}); int t2 = A[i] + T[i] - 1; evt.push_back({t2, 1, i}); } for (int i = 0; i < Q; i++) { int d, l, r; tie(d, l, r) = queries[i]; evt.push_back({d, 9, i}); } vector i2b(N); for (int i = 0; i < N; i++) { i2b[i] = (i / SQ); } int BK = i2b[N - 1] + 1; vector> b2i(BK); for (int i = 0; i < N; i++) { int b = i2b[i]; b2i[b].push_back(i); } vector bleft(BK); vector bright(BK); vector sum(BK); vector diff(BK, 0); for (int b = 0; b < BK; b++) { ll s = 0; for (auto i : b2i[b]) { s += A[i]; } sum[b] = s; bleft[b] = b2i[b][0]; bright[b] = b2i[b].back(); } vector ans(Q); int pre_time = 0; sort(evt.begin(), evt.end()); for (auto e : evt) { int time, kind, ind; tie(time, kind, ind) = e; for (int b = 0; b < BK; b++) { sum[b] += 1ll * diff[b] * (time - pre_time); } if (kind == 9) { int _d, l, r; tie(_d, l, r) = queries[ind]; ll s = 0; for (int b = 0; b < BK; b++) { if (l <= bleft[b] && bright[b] <= r) { s += sum[b]; continue; } if (r < bleft[b]) continue; if (bright[b] < l) continue; for (auto i : b2i[b]) { if (l <= i && i <= r) { ll now = A[i]; if (time >= T[i]) { now -= time - T[i] + 1; if (now < 0) now = 0; } s += now; } } } ans[ind] = s; } else { int d = kind; int b = i2b[ind]; if (d < 0) sum[b]--; diff[b] += d; } pre_time = time; } for (int q = 0; q < Q; q++) { cout << ans[q] << "\n"; } return 0; }