import java.io.PrintWriter;
import java.util.*;
import java.util.stream.Collectors;

public class ProblemC {

    public static void main(String[] args) {

        Scanner sc = new Scanner(System.in);
        PrintWriter pw = new PrintWriter(System.out);

        int T = sc.nextInt();
        sc.nextLine();

        while (T-- > 0) {

            int ll = sc.nextInt();
            sc.nextLine();
            int[] integers = Arrays.stream(sc.nextLine().split(" "))
                    .mapToInt(Integer::parseInt)
                    .toArray();

            solve(integers, ll, pw);

        }

        pw.flush();
        pw.close();
    }

    private static void solve(int[] integers, int ll, PrintWriter pw) {

        HashMap<Integer, TreeSet<Integer>> map = new HashMap<>();

        if (ll == 1) {
            pw.println(-1);
            return;
        }


        int maxL = 0;
        for (int i = 0; i < integers.length; i++) {
            int num = integers[i];
            map.computeIfAbsent(num, f -> new TreeSet<>());
            map.get(num).add(i);

            if (map.get(num).size() > maxL) {
                maxL = map.get(num).size();
            }
        }

        if (maxL == 1) {
            pw.println(-1);
            return;
        }
        ;

        List<TreeSet<Integer>> col = map.values().stream().filter(ts -> ts.size() > 1).collect(Collectors.toList());

        int res = Integer.MAX_VALUE;
        for (TreeSet<Integer> integerTreeSet : col) {
            Iterator<Integer> iterator = integerTreeSet.iterator();
            Integer prev = iterator.next();
            for (; iterator.hasNext(); ) {
                Integer integer = iterator.next();
                if (Math.abs(prev - integer) + 1 < res) {
                    res = Math.abs(prev - integer) + 1;
                }
                prev = integer;
            }
        }

        pw.println(res);

    }


}

