import java.util.*;

public class DominatedSubarray {
    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);
        int test = scanner.nextInt();
        scanner.nextLine();

        for (int t = 0; t < test; t++) {
            int n = scanner.nextInt();
            scanner.nextLine();
            int[] arr = new int[n];
            for (int i = 0; i < n; i++) {
                arr[i] = scanner.nextInt();
            }
            scanner.nextLine();

            Map<Integer, List<Integer>> map = new HashMap<>();
            for (int i = 0; i < n; i++) {
                int num = arr[i];
                if (map.containsKey(num)) {
                    map.get(num).add(i);
                }else{
                    List<Integer> result = new ArrayList<>();
                    result.add(i);
                    map.put(num, result);
                }
            }

            int shortestRange = Integer.MAX_VALUE;
            for (Map.Entry<Integer, List<Integer>> integerListEntry : map.entrySet()) {
                List<Integer> items = integerListEntry.getValue();
                if (items.size() > 1) {
                    int prevItem = items.get(0);
                    for (int i = 1; i < items.size(); i++) {
                        int distance = items.get(i) - prevItem;
                        if (distance < shortestRange) {
                            shortestRange = distance;
                        }
                        prevItem = items.get(i);
                    }
                }
            }
            System.out.println(shortestRange == Integer.MAX_VALUE ? -1 : shortestRange + 1);
        }
    }
}
