#include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include using namespace std; typedef long long ll; typedef vector vi; typedef pair pii; #define MP make_pair #define PB push_back #define inf 1000000007 #define rep(i,n) for(int i = 0; i < (int)(n); ++i) #define all(x) (x).begin(),(x).end() template void Fill(A (&array)[N], const T &val){ std::fill( (T*)array, (T*)(array+N), val ); } template inline bool chmax(T &a, T b){ if(a inline bool chmin(T &a, T b){ if(a>b){ a = b; return true; } return false; } int a[300000]; int res; //区間内の種類の数 multisetst; class Mo{ private: vector left, right,x; const int width; void add(const int id); void del(const int id); public: vector ans; Mo(const int n) : width((int)sqrt(n)){} // クエリ[l,r) void insert(const int l, const int r,const int xx){ left.push_back(l), right.push_back(r); x.push_back(xx); } void solve(){ const int sz = (int)left.size(); int nl = 0, nr = 0; vector ord(sz); iota(ord.begin(), ord.end(), 0); sort(ord.begin(), ord.end(), [&](const int a, const int b){ const int c = left[a] / width, d = left[b] / width; return (c == d) ? ((c & 1) ? (right[b] < right[a]) : (right[a] < right[b])) : (c < d); }); ans.resize(sz); for(const int id : ord){ while(nl > left[id]) add(--nl); while(nr < right[id]) add(nr++); //add while(nl < left[id]) del(nl++); while(nr > right[id]) del(--nr); //del auto k = st.lower_bound(x[id]); int tmp = inf; if(k!=st.end()){ tmp = (*k)-x[id]; } if(k!=st.begin()){ k--; chmin(tmp,x[id]-(*k)); } ans[id] = tmp; } } }; //idは元の配列のインデックス void Mo::add(const int id) { st.insert(a[id]); } void Mo::del(const int id) { st.erase(st.find(a[id])); } int main(){ int n; cin >> n; rep(i,n)cin >> a[i]; int q; cin >> q; Mo mo(n); rep(zz,q){ int l,r,x; cin >> l >> r >> x; mo.insert(l-1,r,x); } mo.solve(); rep(i,q){ cout << mo.ans[i] << "\n"; } return 0; }