#include //#include #define For(i, a, b) for (int(i) = (int)(a); (i) < (int)(b); ++(i)) #define rFor(i, a, b) for (int(i) = (int)(a)-1; (i) >= (int)(b); --(i)) #define rep(i, n) For((i), 0, (n)) #define rrep(i, n) rFor((i), (n), 0) #define fi first #define se second using namespace std; typedef long long lint; typedef unsigned long long ulint; typedef pair pii; typedef pair pll; template bool chmax(T &a, const T &b) { if (a < b) { a = b; return true; } return false; } template bool chmin(T &a, const T &b) { if (a > b) { a = b; return true; } return false; } template T div_floor(T a, T b) { if (b < 0) a *= -1, b *= -1; return a >= 0 ? a / b : (a + 1) / b - 1; } template T div_ceil(T a, T b) { if (b < 0) a *= -1, b *= -1; return a > 0 ? (a - 1) / b + 1 : a / b; } constexpr lint mod = 1000000007; constexpr lint INF = mod * mod; constexpr int MAX = 300010; constexpr int width = 600; int sorted_x[width * 510], x[width * 510]; int get_min(int k, int y, int l, int r) { int ret = mod; For(i, k * width, (k + 1) * width) if (l <= i && i < r) { chmin(ret, abs(x[i] - y)); } return ret; } int get_backet_min(int k, int y) { int l = k * width, r = (k + 1) * width; int i = upper_bound(sorted_x + l, sorted_x + r, y) - sorted_x; int ret = mod; if (i > l) chmin(ret, abs(sorted_x[i - 1] - y)); if (i < r) chmin(ret, abs(sorted_x[i] - y)); return ret; } int main() { int n; scanf("%d", &n); int num = div_ceil(n, width); rep(i, width * num) sorted_x[i] = x[i] = 2 * mod; rep(i, n) { scanf("%d", &x[i]); sorted_x[i] = x[i]; } rep(k, num) sort(sorted_x + k * width, sorted_x + (k + 1) * width); int q; scanf("%d", &q); rep(_, q) { int l, r, y; lint x; scanf("%d%d%d", &l, &r, &y); --l; int ans = mod; rep(k, num) { int ll = k * width, rr = (k + 1) * width; if (r <= ll || rr <= l) continue; else if (l <= ll && rr <= r) chmin(ans, get_backet_min(k, y)); else chmin(ans, get_min(k, y, l, r)); } printf("%d\n", ans); } }