import sys input = sys.stdin.readline from bisect import bisect_left class SegTree: """ define what you want to do with 0 index, ex) size = tree_size, func = min or max, sta = default_value """ def __init__(self,size): self.n = size self.size = 1 << size.bit_length() self.tree = [[-10**10,10**10]]*(2*self.size) def build(self, list): """ set list and update tree""" for i,x in enumerate(list,self.size): self.tree[i] = sorted(self.tree[i]+[x]) for i in range(self.size-1,0,-1): self.tree[i] = sorted(self.tree[i<<1]+self.tree[i<<1 | 1]) def get(self,l,r,x): """ take the value of [l r) with func (min or max)""" l += self.size r += self.size res = 10**10 while l < r: if l & 1: t = bisect_left(self.tree[l],x) res = min(res,x-self.tree[l][t-1],self.tree[l][t]-x) l += 1 if r & 1: t = bisect_left(self.tree[r-1],x) res = min(res,x-self.tree[r-1][t-1],self.tree[r-1][t]-x) l >>= 1 r >>= 1 if res == 0: return res return res n = int(input()) X = list(map(int,input().split())) Q = int(input()) q = [tuple(map(int,input().split())) for i in range(Q)] seg = SegTree(n) seg.build(X) ans = [] for l,r,x in q: ans.append(seg.get(l-1,r,x)) print(*ans,sep="\n")