import java.util.Scanner;

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

        for (int tt = 0; tt < t; tt++) {
            int n = sc.nextInt();
            int[] arr = new int[n];
            for (int i = 0; i < n; i++) arr[i] = sc.nextInt();
            int st = 0;
            int end = 0;
            int min = Integer.MAX_VALUE;
            boolean[] b = new boolean[n+1];
            for (; end < n; end++) {
                int val = arr[end];
                if (!b[val]) {
                    b[val] = true;
                    continue;
                }

                while(arr[st] != val) {
                    b[arr[st]] = false;
                    st++;
                }
                min = Math.min(min, end-st+1);
                st++;
            }
            if (min == Integer.MAX_VALUE) System.out.println(-1);
            else System.out.println(min);
        }
    }
}
