import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.util.StringTokenizer;

public class C {
	static BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
	static StringTokenizer st;
	static PrintWriter out = new PrintWriter(System.out);

	static void solve() throws Exception {
		int tests = nextInt();
		for (int t = 0; t < tests; t++) {
			int n = nextInt();
			Integer[] last = new Integer[n + 1];
//			int[] nums = new int[n];
			int r = Integer.MAX_VALUE;
			for (int i = 0; i < n; i++) {
				int cur = nextInt();
				if (last[cur] != null) {
					r = Math.min(i - last[cur], r);
				}
				last[cur] = i;
			}
			out.println(r == Integer.MAX_VALUE ? -1 : r + 1);
		}
	}

	public static void main(String[] args) throws Exception {
		solve();
		out.close();
	}

	static String next() throws Exception {
		while (st == null || !st.hasMoreTokens()) {
			st = new StringTokenizer(br.readLine());
		}
		return st.nextToken();
	}

	static int nextInt() throws Exception {
		return Integer.parseInt(next());
	}

	static long nextLong() throws Exception {
		return Long.parseLong(next());
	}
}
