import java.util.*;

public class ED_R_76C {

   
    
  public static  int subarr(int arr[], int n){
  	                   int ans = n+1;
  	                   int tmp= n+1;
  	                  // System.out.println("aa");
					Map< Integer,Integer> hm =  
                    new HashMap< Integer,Integer>();   
                        	for(int i=0;i<n;i++){
                        	hm.put(arr[i],-1);
                        	}
                       for(int i=0;i<n;i++){
                       	if(hm.get(arr[i])==-1)
                       		hm.put(arr[i],i);
                       	else{
                       		tmp=i-hm.get(arr[i]);
                       		if(ans>tmp)
                       			ans=tmp;
                       		hm.put(arr[i],i);
                       	}

                       }
                       if(ans==n+1)
                       	ans=-2;
 	return ans+1;
       
    }
	public static void main(String[] args) {
		// TODO Auto-generated method stub
		Scanner sc = new Scanner(System.in);
		int t = Integer.parseInt(sc.nextLine());

		for(int i =0;i<t;i++) {
            int n = Integer.parseInt(sc.nextLine());
            String str = sc.nextLine();
            String[] numbers = str.split(" ");
            int arr[]=new int[n];
            boolean f=false;

            for(int j=0;j<n;j++) {
            	arr[j]=Integer.parseInt(numbers[j]);
            	if(j>0){
            		if(arr[j-1]==arr[j])
            			f=true;
            	}
            }
          if(n<2)
          System.out.println("-1");	
          else if(f)
          	System.out.println("2");	
          else{       

                System.out.println(subarr(arr,n));	

             }
          }						

          
	}

}
	

