import java.io.InputStream;
import java.io.PrintWriter;
import java.util.Locale;
import java.util.Scanner;

public class Problem_C {
	int n;
	int a[] = new int[200002];
	int p[] = new int[200002];
		
	String solve() {
		for (int i = 1; i <= n; i++)
			p[i] = -1;
		int shortest = Integer.MAX_VALUE;
		for (int i = 1; i <= n; i++) {
			int x = a[i];
			if (p[x] != -1) {
				if (i - p[x] + 1 < shortest)
					shortest = i - p[x] + 1;
			}
			p[x] = i;
		}
		return shortest < Integer.MAX_VALUE ? "" + shortest : "-1";
	}

	void parse(Scanner sc) {
		n = sc.nextInt();
		for (int i = 1; i <= n; i++)
			a[i] = sc.nextInt();
	}

	void solveOne(InputStream in, PrintWriter pw) {
		Locale.setDefault(Locale.ROOT);
		try (Scanner sc = new Scanner(in)) {
			int t = sc.nextInt();
			for (int i = 0; i < t; i++) {
				parse(sc);
				pw.println(solve());
			}
		}
	}

	public static void main(String[] args) {
		Locale.setDefault(Locale.ROOT);
		try (PrintWriter pw = new PrintWriter(System.out)) {
			new Problem_C().solveOne(System.in, pw);
			pw.flush();
		}
	}
}