import java.util.Scanner;


public class DominatedSubarray {

	/**
	 * @param args
	 */
	public static void main(String[] args) {
		// TODO Auto-generated method stub

		Scanner sc = new Scanner(System.in);
		int T = sc.nextInt();
		while(T-->0){
			int n = sc.nextInt();
			int arr[] = new int[n+1];
			int ans = Integer.MAX_VALUE;
			boolean flag = false;
			for(int i=1;i<=n;i++){
				int num = sc.nextInt();
				if(arr[num]>0){
					flag = true;
					ans = Math.min(ans, i-arr[num]+1);
				}
				arr[num] = i;
			}
			if(flag)
				System.out.println(ans);
			else
				System.out.println("-1");
		}
		
	}

}
