#include #define rep(i,n) for(int i=0;i<(int)(n);i++) #define chmin(x,y) x = min((x),(y)); #define chmax(x,y) x = max((x),(y)); using namespace std; using ll = long long ; using P = pair ; using pll = pair; const int INF = 1e9; const long long LINF = 1e17; const int MOD = 1000000007; //const int MOD = 998244353; const double PI = 3.14159265358979323846; struct query{ int l,r,y; }; int main(){ int n; cin >> n; vector x(n); rep(i,n) cin >> x[i]; int q; cin >> q; vector l(q),r(q),y(q),qi(q); rep(i,q){ cin >> l[i] >> r[i] >> y[i]; --l[i]; qi[i] = i; } int sq = sqrt(n); sort(qi.begin(),qi.end(),[&](int i,int j){ if(l[i] / sq != l[j] / sq) return l[i] < l[j]; if((l[i] / sq)%2 == 1) return r[i] > r[j]; return r[i] < r[j]; }); multiset st; auto add = [&](int id){ if(id<0||n<=id) return; st.insert(x[id]); }; auto del = [&](int id){ if(id<0||n<=id) return; st.erase(st.find(x[id])); }; vector queryans(q); int nl = 0,nr = 0; for(int i:qi){ while(nl > l[i]) --nl,add(nl); while(nr < r[i]) add(nr),++nr; while(nl < l[i]) del(nl),++nl; while(nr > r[i]) --nr,del(nr); auto it = st.lower_bound(y[i]); int ans = INF; if(it == st.end()){ ans = abs(y[i]-*(--it)); }else if(it == st.begin()){ ans = abs(y[i]-*it); }else{ ans = abs(y[i]-*it); chmin(ans,abs(y[i]-*(--it))); } queryans[i] = ans; } rep(i,q) printf("%d\n",queryans[i]); return 0; return 0; }