#include using namespace std; #define rep(i,n) for(int i = 0; i < (int)(n); ++i) #define rrep(i,n) for(int i = (int)(n) - 1; i >= 0; --i) #define ALL(a) a.begin(), a.end() #define Sort(a) sort(a.begin(), a.end()) #define RSort(a) sort(a.rbegin(), a.rend()) typedef long long int ll; typedef long double ld; typedef vector vi; typedef vector vll; typedef vector vc; typedef vector vst; typedef vector vd; typedef pair P; const long long INF = 0x1fffffffffffffff; const long long MOD = 1000000007; const long double PI = acos(-1); template inline bool chmax(T& a, T b) { if (a < b) { a = b; return 1; } return 0; } template inline bool chmin(T& a, T b) { if (a > b) { a = b; return 1; } return 0; } template inline T vin(T& vec, U n) { vec.resize(n); for(int i = 0; i < (int) n; ++i) cin >> vec[i]; return vec; } template inline void vout(T vec, string s = "\n"){ for(auto x : vec) cout << x << s; } template void in(T&... a){ (cin >> ... >> a); } void out(){ cout << '\n'; } template void out(const T& a, const Ts&... b){ cout << a; (cout << ... << (cout << ' ', b)); cout << '\n'; } template void inGraph(vector>& G, U n, U m, bool directed = false){ G.resize(n); for(int i = 0; i < m; i++){ int a, b; cin >> a >> b; a--, b--; G[a].push_back(b); if(!directed) G[b].push_back(a); } } template struct binary_indexed_tree{ int N; vector BIT; binary_indexed_tree(int N): N(N), BIT(N + 1, 0){ } void add(int i, T x){ i++; while(i <= N){ BIT[i] += x; i += i & -i; } } T sum(int i){ T ans = 0; while(i > 0){ ans += BIT[i]; i -= i & -i; } return ans; } T sum(int L, int R){ return sum(R) - sum(L); } int lower_bound(T x){ if(x <= 0){ return 0; }else{ int v = 0, r = 1; while(r < N) r = r << 1; for(int len = r; len > 0; len = len >> 1){ if(v + len < N && BIT[v + len] < x){ x -= BIT[v + len]; v += len; } } return v; } } int upper_bound(T x){ if(x < 0){ return 0; }else{ int v = 0, r = 1; while(r <= N) r = r << 1; for(int len = r; len > 0; len = len >> 1){ if(v + len <= N && BIT[v + len] <= x){ x -= BIT[v + len]; v += len; } } return v; } } }; ll n, q; vll a, t; void input(){ in(n); a.resize(n); t.resize(n); rep(i, n) in(a[i], t[i]); in(q); } void solve(){ binary_indexed_tree BIT1(n), BIT2(n), BIT3(n), cnt1(n), cnt2(n), cnt3(n); map> mp1, mp2; rep(i, n){ BIT1.add(i, a[i]); cnt1.add(i, 1); if(mp1.count(t[i]) == 0){ mp1[t[i]] = {}; } if(mp2.count(t[i] + a[i]) == 0){ mp2[t[i] + a[i]] = {}; } mp1[t[i]].push_back(i); mp2[t[i] + a[i]].push_back(i); } vector> query(q); rep(i, q){ ll d, l, r; in(d, l, r); query[i] = {d, l, r, i}; } Sort(query); vll ans(q); rep(i, q){ ll d = get<0>(query[i]), l = get<1>(query[i]), r = get<2>(query[i]), k = get<3>(query[i]); l--; while(true){ auto iter = mp1.upper_bound(d); if(iter == mp1.begin()) break; iter--; for(auto x : iter->second){ cnt1.add(x, -1); BIT1.add(x, -a[x]); cnt2.add(x, 1); BIT2.add(x, t[x] + a[x] - 1); } mp1.erase(iter->first); } while(true){ auto iter = mp2.upper_bound(d); if(iter == mp2.begin()) break; iter--; for(auto x : iter->second){ cnt2.add(x, -1); BIT2.add(x, -t[x] -a[x] + 1); } mp2.erase(iter->first); } ll res = BIT1.sum(l, r) + BIT2.sum(l, r) + cnt2.sum(l, r) * (-d); ans[k] = res; } vout(ans, "\n"); } int main(){ ios::sync_with_stdio(false); cin.tie(nullptr); input(); solve(); }