import java.util.Arrays;
import java.util.Scanner;

public class C1257 {
    public static void main(String[] args) {
        Scanner s = new Scanner(System.in);
        int t = s.nextInt();
        for (int i = 0; i < t; i++) {
            int n = s.nextInt();
            int[] a = new int[n];
            for (int j = 0; j < n; j++) {
                a[j] = s.nextInt()-1;
            }
            process(n, a);
        }
    }

    private static void process(int n, int[] a) {
        if (n == 1) {
            System.out.println(-1);
            return;
        }
        int[] pos = new int[n];
        Arrays.fill(pos, Integer.MAX_VALUE);
        int[] last = new int[n];
        Arrays.fill(last, -1);
        for (int i = 0; i < n; i++) {
            if (last[a[i]] != -1)
                pos[a[i]] = Math.min(pos[a[i]], i - last[a[i]] + 1);
            last[a[i]] = i;
        }

        int min = pos[0];
        for (int i = 1; i < n; i++) {
            min = Math.min(min, pos[i]);
        }

        System.out.println(min == Integer.MAX_VALUE ? -1 : min);
    }
}
