import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.HashMap;
 
public class Dom_SubArr {
 
	public static void main(String[] args)throws IOException {
		
		BufferedReader ob=new BufferedReader(new InputStreamReader(System.in));
		StringBuffer sb=new StringBuffer();
		int test=Integer.parseInt(ob.readLine());
		while(test-->0)
		{	
			HashMap<Integer,Integer> ln=new HashMap<Integer,Integer>();
			HashMap<Integer,Integer> ind=new HashMap<Integer,Integer>();
//			int hash[][]=new int[200005][3];
			int n=Integer.parseInt(ob.readLine());
			int a[]=new int[n];
			String s[]=ob.readLine().split(" ");
			if(n==1)
			{
				sb.append("-1\n");
				continue;
			}
			for(int i=0;i<n;i++)
			{
				a[i]=Integer.parseInt(s[i]);
				if(!ind.containsKey(a[i]))
				{
					ind.put( a[i] , i+1);
					ln.put( a[i] , Integer.MAX_VALUE);
				}
				else
				{	
					if( ln.get(a[i])==0 || ( (i+2) - ind.get(a[i])) < ln.get(a[i]) )
					{
						ln.put(a[i] , (i+2)-ind.get(a[i]));
					}
					ind.put(a[i], i+1);
				}
			}
// 			System.out.println("END ** ind = "+ind);
// 			System.out.println("END **** len = "+ln);
			int min=Integer.MAX_VALUE;
			for(int key : ln.keySet())
			{
				if(ln.get(key)<min)
					min=ln.get(key);
			}
			if(min==Integer.MAX_VALUE)
				sb.append("-1\n");
			else
				sb.append(min+"\n");
		}
		System.out.println(sb);
	}
 
}