#include <algorithm>
#include <iostream>
#include <vector>
using namespace std;
const int N = 200000;
const int Q = 200000;
const int N_ = 1 << 18;
const int INF = 0x3f3f3f3f;
int aa[N], ll[N], rr[N];
int ans[Q]; vector<pair<int, int>> qu[N];
int st[N_ << 1], stp[N_ << 1], stq[N_ << 1], lzp[N_], lzq[N_], st_[N_ << 1], h_, n_;
void put(int i, int p, int q) {
st[i] = max(st[i], max(q - stp[i], stq[i] - p));
if (i < n_) {
lzp[i] = min(lzp[i], p);
lzq[i] = max(lzq[i], q);
}
}
void pus(int i) {
if (lzp[i] != INF || lzq[i] != -1) {
int l = i << 1, r = l ^ 1;
put(l, lzp[i], lzq[i]);
put(r, lzp[i], lzq[i]);
lzp[i] = INF;
lzq[i] = -1;
}
}
void pul(int i) {
if (lzp[i] == INF && lzq[i] == -1) {
int l = i << 1, r = l ^ 1;
st[i] = max(st[l], st[r]);
stp[i] = min(stp[l], stp[r]);
stq[i] = max(stq[l], stq[r]);
}
}
void push(int i) {
for (int h = h_; h; h--)
pus(i >> h);
}
void pull(int i) {
while (i >>= 1)
pul(i);
}
void pul_(int i) {
int l = i << 1, r = l ^ 1;
st_[i] = max(st_[l], st_[r]);
}
void build(int n) {
for (h_ = 0, n_ = 1; n_ < n; h_++, n_ <<= 1)
;
for (int i = 0; i < n_; i++) {
st[i + n_] = st_[i + n_] = -1;
stp[i + n_] = lzp[i] = INF;
stq[i + n_] = lzq[i] = -1;
}
for (int i = n_ - 1; i; i--)
pul(i), pul_(i);
}
void update(int l, int r, int b) {
int l_ = l += n_, r_ = r += n_;
push(l_), push(r_);
for ( ; l <= r; l >>= 1, r >>= 1) {
if (l & 1)
put(l++, b, b);
if (!(r & 1))
put(r--, b, b);
}
pull(l_), pull(r_);
}
void update(int i, int a) {
push(i += n_);
if (a != -1)
stp[i] = stq[i] = a;
else {
stp[i] = INF;
stq[i] = -1;
}
pull(i);
}
int query(int l, int r) {
int a = -1;
push(l += n_), push(r += n_);
for ( ; l <= r; l >>= 1, r >>= 1) {
if (l & 1)
a = max(a, st[l++]);
if (!(r & 1))
a = max(a, st[r--]);
}
return a;
}
void update_(int i, int a) {
st_[i += n_] = a;
while (i >>= 1)
pul_(i);
}
int query_(int l, int r) {
int a = -1;
for (l += n_, r += n_; l <= r; l >>= 1, r >>= 1) {
if (l & 1)
a = max(a, st_[l++]);
if (!(r & 1))
a = max(a, st_[r--]);
}
return a;
}
int main() {
ios_base::sync_with_stdio(false), cin.tie(NULL);
int n; cin >> n;
for (int i = 0; i < n; i++)
cin >> aa[i] >> ll[i] >> rr[i];
int q; cin >> q;
for (int h = 0; h < q; h++) {
int l, r; cin >> l >> r, l--, r--;
qu[r].push_back({ l, h });
}
build(n);
for (int r = 0; r < n; r++) {
for (auto e : qu[r])
if (e.first == -1) {
int i = e.second;
if (!(i & 1))
i >>= 1, update(i, aa[i]);
else {
i >>= 1;
update_(i, query(i, i));
update(i, -1);
}
}
update(max(r - rr[r], 0), max(r - ll[r], 0), aa[r]);
for (auto e : qu[r]) {
int l = e.first;
if (l != -1) {
int h = e.second;
ans[h] = max(query(l, r), query_(l, r));
}
}
if (r + ll[r] < n) {
qu[r + ll[r]].push_back({ -1, r << 1 ^ 0 });
if (r + rr[r] + 1 < n)
qu[r + rr[r] + 1].push_back({ -1, r << 1 ^ 1 });
}
}
for (int h = 0; h < q; h++)
cout << ans[h] << '\n';
return 0;
}