import java.io.*;
import java.util.*;
import java.math.*;
 
public class codeforces
{
    public static void main(String[] args)throws IOException
    {
        BufferedReader br =  new BufferedReader(new InputStreamReader(System.in));
        Scanner sc = new Scanner(System.in);
        int t = sc.nextInt();
        while(t-->0)
        {
        	   int n = sc.nextInt();
        	 //  System.out.println("n = "+n);
        	    
        	  
        	    int a[] = new int[n+1];
        	    for(int i=0;i<=n;i++)
        	    {
        	          a[i]=-1;
        	    }
        	    int ans=0;
        	   // if(n<2)
        	   // {
        	   // 	  ans=-1;
        	   // }
        	   // else
        	   // {

		        	   TreeSet<Integer> set = new TreeSet<Integer>();
		        	   for(int i=0;i<n;i++)
		        	   {
		        	   	      int x = sc.nextInt();
		        	   //	      System.out.println("x = "+x);
		        	   	      int dif = i-a[x];
		        	   	      if(a[x]!=-1)
		        	   	      {
		        	   	      
		        	   	      	   set.add(dif+1);
		        	   	      }
		        	   	      a[x]=i;
		        	   } 
		        	   
		        	   for(Integer i : set)
		        	   {
		        	   	     ans=i;
		        	   	     break;
		        	   }
		        	   if(set.isEmpty() || n<2)
		        	   {
		        	   	    ans= -1;
		        	   }
		        	  
		        // } 	  
		         System.out.println(ans);

        	 
        }
     


        
        	
    }




   

}