import java.util.*;
public class Main {
	
	public static void main(String[] args) {
		Scanner sc = new Scanner (System.in);
		int t = sc.nextInt();
		for(int c = 0; c < t; c++) {
			int n = sc.nextInt();
			int occ[] = new int[n + 1];
			for(int i = 1; i <= n; i++) {
				occ[i] = 0;
			}
			int ans = (int)1e9;
			for(int i = 1; i <= n; i++) {
				int a = sc.nextInt();
				if(occ[a] != 0) {
					ans = Math.min(ans, i - occ[a] + 1);
				}
				occ[a] = i;
			}
			System.out.println(ans > n ? -1 : ans);
		}
		sc.close();
	}
}