import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.*;

public class ProblemC76 {
	public static void main(String[] args) throws NumberFormatException, IOException {
		BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
		int ti = Integer.parseInt(br.readLine());
		while (ti-- > 0) {

			int n = Integer.parseInt(br.readLine());
			String[] st = br.readLine().split(" ");
			int[] a = new int[n];
			for (int i = 0; i < n; i++) {
				a[i] = Integer.parseInt(st[i]);
			}

			if (n < 2) {
				System.out.println("-1");
			} else {

				HashMap<Integer, ArrayList<Integer>> hm = new HashMap<>();
				for (int i = 0; i < n; i++) {
					if (!hm.containsKey(a[i])) {
						ArrayList<Integer> ar = new ArrayList<>();
						ar.add(i);
						hm.put(a[i], ar);
					} else {
						ArrayList<Integer> ar = hm.get(a[i]);
						ar.add(i);
						hm.remove(a[i]);
						hm.put(a[i], ar);
					}
				}
				int min = Integer.MAX_VALUE;
				for (int i : hm.keySet()) {

					ArrayList<Integer> ar = hm.get(i);
					for (int j = 1; j < ar.size(); j++) {
						if (ar.get(j) - ar.get(j - 1) +1 < min)
							min = ar.get(j) - ar.get(j - 1) +1;
					}
				}
				System.out.println(min);
			}

		}

	}
}
