

import java.util.*;

public class Main {

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

        for (int i = 0; i < n; i++) {
            Map<Integer, Integer> recurrence = new HashMap<>();
            int length = in.nextInt();
            int[] array = new int[length];
            int recNum = 0;

            for (int j = 0; j < length; j++) {
                int number = in.nextInt();
                recurrence.putIfAbsent(number, 1);
                recurrence.put(number, recurrence.get(number) + 1);
                array[j] = number;
            }

            List<Map.Entry<Integer, Integer>> list = new ArrayList<>(recurrence.entrySet());
            list.sort(Map.Entry.comparingByValue());

            if (list.size() > 2) {
                int last = list.get(list.size() - 1).getValue();
                int prev = list.get(list.size() - 2).getValue();

                if (last == prev) {
                    System.out.println(-1);
                    continue;
                } else {
                    recNum = list.get(list.size() - 1).getKey();
                }
            } else {
                recNum = list.get(list.size() - 1).getKey();
            }

            int minSubArrayLen = Integer.MAX_VALUE;
            for (int j = 0; j < length - 1; j++) {
                if (array[j] != recNum) {
                    continue;
                }

                int currentLen = 1;
                for (int k = j + 1; k < length; k++) {
                    currentLen++;
                    if (array[k] == recNum) {
                        break;
                    }
                }

                minSubArrayLen = Math.min(minSubArrayLen, currentLen);
            }

            if (minSubArrayLen == Integer.MAX_VALUE) {
                System.out.println(-1);
            } else {
                System.out.println(minSubArrayLen);
            }
        }
    }
}
