
import java.util.HashMap;
import java.util.Scanner;

public class DOMINATED_SUBARRAY {

	public static void main(String[] args) {

		Scanner nik = new Scanner(System.in);
		int t = nik.nextInt();
		StringBuilder sb = new StringBuilder();
		
		while(t-->0) {
		int n = nik.nextInt();
			HashMap<Integer, Integer> map = new HashMap<>();
			int[] a = new int[n];
			for (int i = 0; i < n; i++) {
				a[i] = nik.nextInt();

			} 
			int l = Integer.MAX_VALUE;
			for (int i = 0; i < n; i++) {
				int v = a[i];
				if (map.containsKey(v)) {
					l = Math.min(l, i - map.get(v) + 1);
					map.put(v, i);
				} else {
					map.put(v, i);
				}
			}
			sb.append((l == Integer.MAX_VALUE ? -1 : l) + "\n");

		}
		System.out.println(sb);

	}

}