import java.util.HashMap;
import java.util.Scanner;

public class C2 {
    public static void main(String[] args) {
        Scanner in = new Scanner(System.in);

        int cases = Integer.parseInt(in.nextLine());
        for (int i = 0; i < cases; i++) {
            int n = Integer.parseInt(in.nextLine());
            String[] array = in.nextLine().split(" ");
            int[] a = new int[n];
            for (int j = 0; j < n; j++) {
                a[j] = Integer.parseInt(array[j]);
            }

            int minDist = Integer.MAX_VALUE;
            HashMap<Integer, Integer> numIdxMap = new HashMap<>();
            for (int j = 0; j < n; j++) {
                if (numIdxMap.containsKey(a[j])) {
                    minDist = Math.min(minDist, j - numIdxMap.get(a[j]) + 1);
                }

                numIdxMap.put(a[j], j);
            }

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