import java.util.Scanner;

public class Main{
	static int N = 200005;
	public static void main(String args[]) {
		Scanner cin=new Scanner(System.in);
		int T=cin.nextInt();
		while(T-->0) {
			int n=cin.nextInt();
			int[] arr=new int[N];
			int[] lst=new int[N];
			for(int i=1;i<=n;i++) { 
				arr[i]=cin.nextInt();lst[i]=-1;
			}
			int lc=5*N;
			for(int i=1;i<=n;i++){
				if(lst[arr[i]]!=-1){
					lc=Math.min(lc,i-lst[arr[i]]+1);
				}
				lst[arr[i]]=i;
			}
			if(lc==5*N) System.out.println("-1");
			else System.out.println(lc);
		}
	}
}

					 	 			  			 					   	   	