import java.util.*;

public class Main {

    public static void main(String[] args) {
        final Scanner scanner = new Scanner(System.in);
        solve(scanner);
    }


    // int N = scanner.nextInt();
    // scanner.nextLine();
    // int[] n = Arrays.stream(scanner.nextLine().split(" ")).mapToInt(Integer::parseInt).toArray();
    static void solve(Scanner scanner) {
        int N = Integer.parseInt(scanner.nextLine());
        loop:
        for (int i = 0; i < N; i++) {
            int n = Integer.parseInt(scanner.nextLine());
            int[] a = Arrays.stream(scanner.nextLine().split(" ")).mapToInt(Integer::parseInt).toArray();
            Map<Integer, Integer> valToMinIndex = new HashMap<>();
            int ans = Integer.MAX_VALUE;
            for (int j = 0; j < a.length; j++) {
                if (valToMinIndex.containsKey(a[j])) {
                    ans = Math.min(ans, j - valToMinIndex.get(a[j]) + 1);
                    valToMinIndex.put(a[j], j);
                }
                valToMinIndex.put(a[j], j);
            }
            System.out.println(ans == Integer.MAX_VALUE ? -1 : ans);
        }
    }

}
