import sys input = sys.stdin.readline N = int(input()) a = list(map(int, input().split())) Q = int(input()) ls = [0] * Q rs = [0] * Q xs = [0] * Q qs = [] for i in range(Q): l, r, x = map(int, input().split()) l -= 1 ls[i] = l rs[i] = r xs[i] = x qs.append(i) qs.sort(key = lambda x: xs[x]) N0 = 2**(N-1).bit_length() INF = 2**31-1 data = [0]*(2*N0) def update(k, x): k += N0-1 data[k] = x while k >= 0: k = (k - 1) // 2 data[k] = max(data[2*k+1], data[2*k+2]) def query(l, r): L = l + N0; R = r + N0 s = 0 while L < R: if R & 1: R -= 1 s = max(s, data[R-1]) if L & 1: s = max(s, data[L-1]) L += 1 L >>= 1; R >>= 1 return s s = [] for i in range(N): s.append((a[i], i)) s.sort(reverse = True) inf = 10 ** 10 res = [inf] * Q for i in qs: l, r, x = ls[i], rs[i], xs[i] while len(s) and s[-1][0] <= x: y, j = s.pop() update(j, y) q = query(l, r) if q: res[i] = x - q #print(res) # N: 処理する区間の長さ N0 = 2**(N-1).bit_length() INF = 2**31-1 data = [INF]*(2*N0) # a_k の値を x に更新 def update(k, x): k += N0-1 data[k] = x while k >= 0: k = (k - 1) // 2 data[k] = min(data[2*k+1], data[2*k+2]) # 区間[l, r)の最小値 def query(l, r): L = l + N0; R = r + N0 s = INF while L < R: if R & 1: R -= 1 s = min(s, data[R-1]) if L & 1: s = min(s, data[L-1]) L += 1 L >>= 1; R >>= 1 return s s = [] for i in range(N): s.append((a[i], i)) s.sort() qs.reverse() for i in qs: l, r, x = ls[i], rs[i], xs[i] while len(s) and s[-1][0] >= x: y, j = s.pop() update(j, y) q = query(l, r) if q: res[i] = min(res[i], q - x) for r in res: print(r)