				
import java.util.*;import java.io.*;import java.math.*;
public class Main
{

	public static void process()throws IOException
    {
    	int n=ni(),arr[]=new int[n+1],f[]=new int[200001];

    	for(int i=1;i<=n;i++){
    		arr[i]=ni();
    		f[arr[i]]++;
    	} 

    	HashSet<Integer> set = new HashSet<Integer>();
    	int cnt=0;
    	if(n==1){
    		pn("-1");
    		return;
    	}
    	for(int i=1;i<=n;i++){
    		if(!set.contains(arr[i])){
    			set.add(arr[i]);
    		}
    	}
    	if(set.size()==n){
    		pn("-1");
    		return;
    	}
    	// int maxtmp=-1,mxcnt=0;
    	// for(int i : f){
    	// 	if(i==0)
    	// 		continue;
    	// 	maxtmp=Math.max(maxtmp,i);
    	// }
    	Arrays.fill(f,0);
    	int l=1,min_len=Integer.MAX_VALUE;
    	for(int i=1;i<=n;){
    		f[arr[i]]++;
    		if(f[arr[i]]==2){
    			//pn(i+" "+l);
    			int tlen=i-l+1;
    			min_len=Math.min(min_len,tlen);

    			while(l<i){
    				f[arr[l]]--;
    				l++;
    			}
    			l=i;i++;
    			// while(f[arr[i]]!=2 && i<=n){
    			// 	f[arr[l]]--;
    			// 	l++;
    			// 	f[arr[i]]++;
    			// 	i++;
    			// }

    			// if(i>n && min_len==0){
    			// 	pn("-1");
    			// }
    			// else{
    			// 	min_len=Math.min(min_len, i-l+1);
    			// }
    		}
    		else
    			i++;
    	}
    	pn(min_len);	
    }

    static FastReader sc=new FastReader();
    //static AnotherReader sc;

    public static void main(String[]args)throws IOException
    {
        //sc=new AnotherReader();
        int t=ni();
        while(t-->0)
            process();
        System.out.flush();
        System.out.close();  
    }

    static void pn(Object o){System.out.println(o);}
    static void p(Object o){System.out.print(o);}
    static void pni(Object o){System.out.println(o);System.out.flush();}
    static int ni()throws IOException{return sc.nextInt();}
    static long nl()throws IOException{return sc.nextLong();}
    static double nd()throws IOException{return sc.nextDouble();}
    static String nln()throws IOException{return sc.nextLine();}
    static long gcd(long a, long b)throws IOException{return (b==0)?a:gcd(b,a%b);}
    static int gcd(int a, int b)throws IOException{return (b==0)?a:gcd(b,a%b);}
    static int bit(long n)throws IOException{return (n==0)?0:(1+bit(n&(n-1)));}
    static boolean multipleTC=false;



/////////////////////////////////////////////////////////////////////////////////////////////////////////
    static class FastReader{BufferedReader br;StringTokenizer st;public FastReader()
    {br=new BufferedReader(new InputStreamReader(System.in));}String next()
    {while(st==null||!st.hasMoreElements())
    {try{st=new StringTokenizer(br.readLine());}
    catch(IOException e){e.printStackTrace();}}
    return st.nextToken();}int nextInt()
    {return Integer.parseInt(next());}long nextLong()
    {return Long.parseLong(next());}double nextDouble()
    {return Double.parseDouble(next());}
    String nextLine(){String str="";try
    {str=br.readLine();}catch(IOException e)
    {e.printStackTrace();}return str;}}

    static class AnotherReader{BufferedReader br; StringTokenizer st;
    AnotherReader()throws FileNotFoundException{
    br = new BufferedReader(new FileReader("input.txt"));}
    String next()throws IOException{
    while (st == null || !st.hasMoreElements()) {try{
    st = new StringTokenizer(br.readLine());}
    catch (IOException  e){ e.printStackTrace(); }}
    return st.nextToken(); } int nextInt() throws IOException{
    return Integer.parseInt(next());}
    long nextLong() throws IOException
    {return Long.parseLong(next());}
    double nextDouble()throws IOException { return Double.parseDouble(next()); }
    String nextLine() throws IOException{ String str = ""; try{
    str = br.readLine();} catch (IOException e){
    e.printStackTrace();} return str;}}
/////////////////////////////////////////////////////////////////////////////////////////////////////////////
}
	
	