import java.util.*;
public class Codeforces {
	public static void main(String args[]) {
    	Scanner in = new Scanner(System.in);
    	
    	int t = in.nextInt();
    	while(t-->0) {
    		int n = in.nextInt();
    		int a[] = new int[n];
    		
    		for(int i=0; i<n; i++) a[i] = in.nextInt();
    		
    		int[][] vis = new int[n+1][2];
    		int min = Integer.MAX_VALUE; int rep = 0;
    		for(int i=0; i<n; i++) {
    			if(vis[a[i]][0] == 1) {
    				min = Math.min(min, i-vis[a[i]][1]+1);
    				vis[a[i]][1] = i;
    				rep = 1;
    			}
    			else {
    				vis[a[i]][0] = 1;
    				vis[a[i]][1] = i;
    			}
    		}
    		
    		System.out.println(rep==0 ? -1 : min);
    		
    	}
    	
	}
}