
import java.util.Scanner;

public class C {
//4 1 2 4 5 4 3 2 1

	public static void main(String[] args) {
		// TODO Auto-generated method stub
			Scanner in =new Scanner(System.in);
			int t=in.nextInt();
			for(int qq=0;qq<t;qq++) {
				int n=in.nextInt();
			int []arr=new int[n];
			for(int i=0;i<n;i++) {
				arr[i]=in.nextInt();
			}int []arr2=new int[n+1];	
			int []arr3=new int[n+1];
			for(int i=0;i<n;i++) {
				if(arr2[arr[i]]!=0) {
					if(arr3[arr[i]]!=0) {
					arr3[arr[i]]=Math.min(arr3[arr[i]], i-arr2[arr[i]]+1);}
					else {
						arr3[arr[i]]=i-arr2[arr[i]]+1;
					}
				}
				arr2[arr[i]]=i+1;
			}int max=99999999;
			for(int i=0;i<n+1;i++) {
				if(arr3[i]!=0) {
				max=Math.min(max, arr3[i]);
				}			}
			if(max==99999999) {
				System.out.println(-1);
			}else {
				System.out.println(max+1);
			}
			

			}
	}

}
