using System;
using System.Collections.Generic;
using System.Collections;
using System.Collections.Specialized;
using System.Linq;
using System.Text;
using System.IO;
using System.Reflection;
using static System.Math;
using System.Numerics;
static class Program{
	const int mod=(int)1e9+7;
	const double eps=1e-11;
	static void Main(){
		Sc sc=new Sc();
		var n=sc.I;
		var a=sc.Ia;
		var wm=new Wm(a);
		int q=sc.I;
		StringBuilder sb=new StringBuilder();
		int p=a.Min();
		for(int i = 0;i<q;i++) {
			var e=sc.Ia;
			if(e[2]==0){sb.Append(p+"\n");}
			else{sb.Append(Min(Abs(e[2]-wm.Pv(e[0],e[1],e[2]+1)),Abs(e[2]-wm.Nv(e[0],e[1],e[2]-1)))+"\n");}
		}
		Console.Write(sb);
	}
}
public class Wm{
	private int[][] ra;
	private int[] za;
	private const int b=19;
	private int n;
	public Wm(int[] a){
		n=a.Length;
		var d=new int[b+1][];
		ra=new int[b+1][];
		for(int i = 0;i<=b;i++) {
			d[i]=new int[n];
			ra[i]=new int[n+1];
		}
		var d0=new int[2][]{new int[n],new int[n]};
		var d1=new int[2][]{new int[n],new int[n]};
		var h0=new int[]{0,0};
		var h1=new int[]{0,0};
		za=new int[b+1];
		for(int i = 0;i<n;i++) {
			if((a[i]&(1<<b))==0){
				d0[1][h0[1]]=a[i];
				h0[1]++;
				za[b]++;
			}
			else{
				d1[1][h1[1]]=a[i];
				h1[1]++;
				d[b][i]=ra[b][i+1]=1;
			}
			ra[b][i+1]+=ra[b][i];
		}
		for(int i = 1;i<=b;i++) {
			int g=(1<<(b-i)),p=i&1;
			h0[p^1]=h1[p^1]=0;
			for(int j = 0;j<h0[p];j++) {
				if((d0[p][j]&g)==0){
					d0[p^1][h0[p^1]]=d0[p][j];
					h0[p^1]++;
					za[b-i]++;
				}
				else{
					d1[p^1][h1[p^1]]=d0[p][j];
					h1[p^1]++;
					d[b-i][j]=ra[b-i][j+1]=1;
				}
				ra[b-i][j+1]+=ra[b-i][j];
			}
			for(int j = 0;j<h1[p];j++) {
				if((d1[p][j]&g)==0){
					d0[p^1][h0[p^1]]=d1[p][j];
					h0[p^1]++;
					za[b-i]++;
				}
				else{
					d1[p^1][h1[p^1]]=d1[p][j];
					h1[p^1]++;
					d[b-i][h0[p]+j]=ra[b-i][h0[p]+j+1]=1;
				}
				ra[b-i][h0[p]+j+1]+=ra[b-i][h0[p]+j];
			}
		}
	}
	public int Rk(int l,int r,long k){
		l--;
		for(int i = b;i>=0;i--) {
			if(((k>>i)&1)==1){
				r=ra[i][r+1]+za[i]-1;
				l=ra[i][l+1]+za[i]-1;
			}
			else{
				r=r-ra[i][r+1];
				l=l-ra[i][l+1];
			}
		}
		return r-l;
	}
	public int Rl(int l,int r,long k){
		l--;
		int p=0;
		for(int i = b;i>=0;i--) {
			if(((k>>i)&1)==1){
				p+=r-l;
				r=ra[i][r+1]+za[i]-1;
				l=ra[i][l+1]+za[i]-1;
				p-=r-l;
			}
			else{
				r=r-ra[i][r+1];
				l=l-ra[i][l+1];
			}
		}
		return p;
	}
	public int Rm(int l,int r,long k){
		l--;
		int p=0;
		for(int i = b;i>=0;i--) {
			if(((k>>i)&1)==1){
				r=ra[i][r+1]+za[i]-1;
				l=ra[i][l+1]+za[i]-1;
			}
			else{
				p+=r-l;
				r=r-ra[i][r+1];
				l=l-ra[i][l+1];
				p-=r-l;
			}
		}
		return p;
	}
	public int Qt(int l,int r,int k){
		l--;
		int p=0;
		for(int i = b;i>=0;i--) {
			if((r-ra[i][r+1])-(l-ra[i][l+1])<k){
				k-=(r-ra[i][r+1])-(l-ra[i][l+1]);
				r=ra[i][r+1]+za[i]-1;
				l=ra[i][l+1]+za[i]-1;
				p|=1<<i;
			}
			else{
				r=r-ra[i][r+1];
				l=l-ra[i][l+1];
			}
		}
		return p;
	}
	public int Pv(int l,int r,int k){
		int p=Rl(l,r,k);
		return p!=0?Qt(l,r,p):1000000000;
	}
	public int Nv(int l,int r,int k){
		int p=Rm(l,r,k);
		return p!=0?Qt(l,r,r-l+1-p+1):1000000000;
	}
}
public class Sc{
	public int I{get{return int.Parse(Console.ReadLine());}}
	public long L{get{return long.Parse(Console.ReadLine());}}
	public double D{get{return double.Parse(Console.ReadLine());}}
	public string S{get{return Console.ReadLine();}}
	public int[] Ia{get{return Array.ConvertAll(Console.ReadLine().Split(),int.Parse);}}
	public long[] La{get{return Array.ConvertAll(Console.ReadLine().Split(),long.Parse);}}
	public double[] Da{get{return Array.ConvertAll(Console.ReadLine().Split(),double.Parse);}}
	public string[] Sa{get{return Console.ReadLine().Split();}}
	public object[] Oa{get{return Console.ReadLine().Split();}}
	public int[] Ia2{get{return Array.ConvertAll(("0 "+Console.ReadLine()+" 0").Split(),int.Parse);}}
	public int[] Ia3(int a){return Array.ConvertAll((a.ToString()+" "+Console.ReadLine()).Split(),int.Parse);}
	public int[] Ia3(bool a,int b,bool c,int d){return Array.ConvertAll(((a?b.ToString()+" ":"")+Console.ReadLine()+(c?" "+d.ToString():"")).Split(),int.Parse);}
	public long[] La2{get{return Array.ConvertAll(("0 "+Console.ReadLine()+" 0").Split(),long.Parse);}}
	public long[] La3(int a){return Array.ConvertAll((a.ToString()+" "+Console.ReadLine()).Split(),long.Parse);}
	public long[] La3(bool a,int b,bool c,int d){return Array.ConvertAll(((a?b.ToString()+" ":"")+Console.ReadLine()+(c?" "+d.ToString():"")).Split(),long.Parse);}
	public double[] Da2{get{return Array.ConvertAll(("0 "+Console.ReadLine()+" 0").Split(),double.Parse);}}
	public double[] Da3(int a){return Array.ConvertAll((a.ToString()+" "+Console.ReadLine()).Split(),double.Parse);}
	public double[] Da3(bool a,int b,bool c,int d){return Array.ConvertAll(((a?b.ToString()+" ":"")+Console.ReadLine()+(c?" "+d.ToString():"")).Split(),double.Parse);}
	public T[] Arr<T>(int n,Func<T> f){var a=new T[n];for(int i=0;i<n;i++){a[i]=f();}return a;}
	public T[] Arr<T>(int n,Func<int,T> f){var a=new T[n];for(int i=0;i<n;i++){a[i]=f(i);}return a;}
	public T[] Arr<T>(int n,Func<string[],T> f){var a=new T[n];for(int i=0;i<n;i++){a[i]=f(Console.ReadLine().Split());}return a;}
	public T[] Arr<T>(int n,Func<int,string[],T> f){var a=new T[n];for(int i=0;i<n;i++){a[i]=f(i,Console.ReadLine().Split());}return a;}
}


Measured memory_bytes: 47900