import java.io.*;
import java.util.*;

public class CF1257C extends PrintWriter {
	CF1257C() { super(System.out); }
	Scanner sc = new Scanner(System.in);
	public static void main(String[] $) {
		CF1257C o = new CF1257C(); o.main(); o.flush();
	}

	void main() {
		int t = sc.nextInt();
		while (t-- > 0) {
			int n = sc.nextInt();
			int[] ii = new int[n]; Arrays.fill(ii, -1);
			int ans = n + 1;
			for (int i = 0; i < n; i++) {
				int a = sc.nextInt() - 1;
				if (ii[a] != -1)
					ans = Math.min(ans, i - ii[a] + 1);
				ii[a] = i;
			}
			if (ans == n + 1)
				ans = -1;
			println(ans);
		}
	}
}
