#include using namespace std; #define rep(i,n) for(int i = 0; i < (n); ++i) #define rrep(i,n) for(int i = (n)-1; i >= 0; --i) #define chmax(a, b) a = max(a, b) #define chmin(a, b) a = min(a, b) #define all(x) (x).begin(), (x).end() #define rall(x) (x).rbegin(), (x).rend() using ll = long long; using P = pair; using VI = vector; using VVI = vector; using VL = vector; using VVL = vector; int main() { ios::sync_with_stdio(false); cin.tie(0); int n; cin >> n; VI x(n); rep(i, n) cin >> x[i]; struct Q { int i; int l, r, x; bool operator<(Q& y) { int bx = l / 550, by = y.l / 550; return bx < by || bx == by && r < y.r; } }; int q; cin >> q; vector qs(q); rep(i, q) { qs[i].i = i; cin >> qs[i].l >> qs[i].r >> qs[i].x; qs[i].l--; } sort(all(qs)); int cur_b = -1; VI ans(q); multiset s; int p = 0, v = 0; rep(i, q) { int l = qs[i].l, r = qs[i].r, z = qs[i].x; int b = l / 550; if (b != cur_b) { cur_b = b; s.clear(); p = l, v = l + 1; s.insert(x[l]); } for(; v != r; ++v) { s.insert(x[v]); } while(l < p) { s.insert(x[--p]); } while(l > p) { s.erase(s.find(x[p++])); } auto it = s.lower_bound(z); int tans = 1001001001; if (it != s.end()) { chmin(tans, *it - z); } if (it != s.begin()) { it--; chmin(tans, z - *it); } ans[qs[i].i] = tans; } rep(i, q) cout << ans[i] << " \n"[i + 1 == q]; }