import java.util.*;

public class C {
	static Scanner sc = new Scanner(System.in);

	static void test() {
		int n = sc.nextInt();
		ArrayList<Integer>[] indexes = (ArrayList<Integer>[])new ArrayList[n + 1];
		int x;
		for (int i = 0; i < n; i++) {
			x = sc.nextInt();
			if (indexes[x] == null) indexes[x] = new ArrayList<Integer>();
			indexes[x].add(i);	
		}
		int res = Integer.MAX_VALUE;
		for (int i = 1; i <= n; i++) {
			if (indexes[i] != null && indexes[i].size() > 1) {
				for (int j = 1; j < indexes[i].size(); j++) {
					int size = indexes[i].get(j) - indexes[i].get(j - 1) + 1;
					if (size < res)
						res = size;
				}
			}
		}
		System.out.println(res == Integer.MAX_VALUE ? -1 : res);
	}
	
	public static void main(String[] args) {
		int t = sc.nextInt();
		while (t-- > 0) test();
	}
}
