

import java.io.*;
import java.math.*;
import java.util.*;


// @author : Dinosparton [ SHIVAM BHUVA ]

public class test {
	 
	   static class Pair{ 
		   int x;
		   int y;
		   
		   Pair(int x,int y){ 
			   this.x = x;
			   this.y = y;
			   
		   }
	   }
	  
	   static class Compare { 
		   
		     void compare(Pair arr[], int n) 
		    { 
		        // Comparator to sort the pair according to second element 
		        Arrays.sort(arr, new Comparator<Pair>() { 
		            @Override public int compare(Pair p1, Pair p2) 
		            { 
		                return p1.x - p2.x; 
		            } 
		        }); 
		  
//		        for (int i = 0; i < n; i++) { 
//		            System.out.print(arr[i].x + " " + arr[i].y + " "); 
//		        } 
//		        System.out.println(); 
		    } 
		} 
	  
	   public static void main(String args[]) { 
		
		  Scanner sc = new Scanner(System.in);
		
		  StringBuffer res = new StringBuffer();
		  int tc= sc.nextInt();
		  
	        while(tc-->0) {
	 
	        	int n = sc.nextInt();
	        	
	        	int a[] = new int[n];
	        	
	        	HashMap<Integer,Integer> map = new HashMap<>();
	        	int min = Integer.MAX_VALUE;
	        	
	        	for(int i=0;i<n;i++) { 
	        		a[i] = sc.nextInt();
	        		
	        		if(map.containsKey(a[i])) { 
	        			int k = map.get(a[i]);
	        			min = Math.min(min, i -k +1);
	        			map.put(a[i], i);
	        		}
	        		else
	        			map.put(a[i], i);
	        	}
	        	
	        	if(min==Integer.MAX_VALUE) { 
	        		res.append(-1).append("\n");
	        	}
	        	else { 
	        		res.append(min).append("\n");
	        	}
	        }
	        
	        System.out.println(res);
	   }  
}  




