import java.util.Scanner;
import java.util.Vector;

public class C {
    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);
        int t = scanner.nextInt();

        Vector<Integer> vectorA = new Vector<>();
        Vector<Integer> vectorB = new Vector<>();
        for (int i = 0; i < t; i++) {
            // read
            int n = scanner.nextInt();
            vectorA.clear();
            for (int j = 0; j < n; j++)
                vectorA.add(scanner.nextInt());
            // solve
            int answer = n + 5;
            vectorB.clear();
            vectorB.setSize(n + 1);
            for (int j = 0; j < n; j++) {
                final Integer fromA = vectorA.get(j);
                final Integer fromB = vectorB.get(fromA);
                if (fromB != null && fromB != -1) {
                    answer = Math.min(answer, j - fromB + 1);
                }
                vectorB.set(fromA, j);
            }
            if (answer > n) answer = -1;
            System.out.println(answer);
        }
        scanner.close();
    }
}
