
//normal
import java.util.*;
import java.lang.*;
import java.io.*;
// String Tokenizer

public class Main {
	public static void main(String[] args) {
		// code
		Scanner scn = new Scanner(System.in);
	
		int t = scn.nextInt();
		while (t > 0) {
			t--;
			int n = scn.nextInt();
			int[] arr = new int[n];
			for (int i = 0; i < n; i++) {
				arr[i] = scn.nextInt();
			}

			HashMap<Integer, ArrayList> map = new HashMap<>();
			for (int i = 0; i < n; i++) {
				int ch = arr[i];
				if (map.containsKey(ch)) {
					ArrayList<Integer> xp = map.get(ch);
					xp.add(i);
					map.put(ch, xp);
				} else {
					ArrayList<Integer> xp = new ArrayList<>();
					xp.add(i);
					map.put(ch, xp);
				}
			}
			int min = 999999999;
			for (Integer x : map.keySet()) {
				ArrayList<Integer> xp = map.get(x);
				for (int i = 0; i < xp.size() - 1; i++) {
					int temp = (xp.get(i + 1) - xp.get(i));
					if (temp < min) {
						min = temp;
					}
				}
			}
			if (min == 999999999) {
				System.out.println(-1);
			} else {
				System.out.println(min+1);
			}
		}
	}
}