import java.util.Scanner;

public class C1257 {

    public static void main(String[] args) {
        Scanner in = new Scanner(System.in);
        int T = in.nextInt();
        for (int t=0; t<T; t++) {
            int N = in.nextInt();
            int[] last = new int[N];
            int answer = Integer.MAX_VALUE;
            for (int n=1; n<=N; n++) {
                int a = in.nextInt()-1;
                if (last[a] != 0) {
                    answer = Math.min(answer, n-last[a]+1);
                }
                last[a] = n;
            }
            System.out.println(answer == Integer.MAX_VALUE ? -1 : answer);
        }
    }

}
