
import java.util.Arrays;
import java.util.HashMap;
import java.util.HashSet;
import java.util.Scanner;

public class codeforce {

	public static void main(String[] args) {

		
		Scanner sc = new Scanner(System.in);
		
	    int tc = sc.nextInt();
	    while(tc-- > 0) {
	   
	    	int a = sc.nextInt();
            int [] input = new int[a];
            int [] sortedInput = new int[a];

            
	    	for(int i=0;i<a;i++) {
	    		input[i] = sc.nextInt();
	    		sortedInput[i] = input[i] ;
	    	}
	    	
	    	Arrays.sort(sortedInput);
	    	HashSet <Integer> doubleElements = new HashSet<Integer>();
	    	for(int i=1;i<sortedInput.length;i++) {
	    		if(sortedInput[i] == sortedInput[i-1]) {
	    			doubleElements.add(sortedInput[i]);
	    		}
	    		
	    		
	    	}
	    	HashMap <Integer,Integer> numbPos = new HashMap();
            int min = Integer.MAX_VALUE; 
            
	    	
	    	for(int i=0; i<input.length;i++) {
	    		
	    		if(numbPos.containsKey(input[i])) {
	    			
	    			if(min > i - numbPos.get(input[i])) {
	    				min = i - numbPos.get(input[i]);
	    				
	    					}
	    			numbPos.put(input[i], i);	
	    				
	    			
	    		}
	    		else if(doubleElements.contains(input[i])) {
	    			numbPos.put(input[i], i);
	    		}
	    		
	    		
	    	}
	    	
	    	min = min == Integer.MAX_VALUE ? -1 : min+1;
	    	System.out.println(min);
	    }
	    
	
	}
	
}