#include using namespace std; const int B = 400; const int INF = 1000000000; int main(){ int N; cin >> N; vector X(N); for (int i = 0; i < N; i++){ cin >> X[i]; } int Q; cin >> Q; vector l(Q), r(Q), x(Q); for (int i = 0; i < Q; i++){ cin >> l[i] >> r[i] >> x[i]; l[i]--; } vector> query(Q); for (int i = 0; i < Q; i++){ if (l[i] / B % 2 == 0){ query[i] = make_tuple(l[i] / B, r[i], l[i], i); } else { query[i] = make_tuple(l[i] / B, -r[i], l[i], i); } } sort(query.begin(), query.end()); vector ans(Q, INF); set st; int L = 0, R = 0; for (int i = 0; i < Q; i++){ int l2 = get<2>(query[i]); int r2 = abs(get<1>(query[i])); int id = get<3>(query[i]); while (l2 < L){ L--; st.insert(X[L]); } while (R < r2){ st.insert(X[R]); R++; } while (L < l2){ st.erase(X[L]); L++; } while (r2 < R){ R--; st.erase(X[R]); } auto itr = st.lower_bound(x[id]); if (itr != st.end()){ ans[id] = min(ans[id], *itr - x[id]); } if (itr != st.begin()){ ans[id] = min(ans[id], x[id] - *prev(itr)); } } for (int i = 0; i < Q; i++){ cout << ans[i] << endl; } }