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

public class Ex1257C {

	static StreamTokenizer in;

	static int nextInt() throws IOException {
		in.nextToken();
		return (int) in.nval;
	}

	static void skip(int count) throws IOException {
		while (count-- > 0)
			in.nextToken();
	}

	public static void main(String[] args) throws Exception {
		in = new StreamTokenizer(new BufferedReader(new InputStreamReader(System.in)));
		PrintWriter out = new PrintWriter(System.out);

		int lastPos[] = new int[200001];
		int t = nextInt();
		for (int ti = 0; ti < t; ti++) {
			int n = nextInt();
			if (n == 1) {
				in.nextToken();
				out.println(-1);
				continue;
			}
			int min = n + 1;
			Arrays.fill(lastPos, 1, n + 1, -1);
			for (int i = 0; i < n; i++) {
				int ai = nextInt();
				if (lastPos[ai] != -1 && i - lastPos[ai] + 1 < min) {
					min = i - lastPos[ai] + 1;
					if (min == 2) {
						skip(n - i - 1);
						break;
					}
				}
				lastPos[ai] = i;
			}
			out.println(min == n + 1 ? -1 : min);
		}
		out.flush();
	}
}