
import javafx.util.Pair;

import java.io.*;
import java.lang.*;
import java.util.*;

public class Solver {
    public static void main(String[] args) {
        FastReader in = new FastReader();
        PrintWriter out = new PrintWriter(System.out);
        TaskC solver = new TaskC(in, out);
        solver.solveC();
        out.close();
    }

    static class TaskC {
        FastReader in;
        PrintWriter out;

        public TaskC(FastReader in, PrintWriter out) {
            this.in = in;
            this.out = out;
        }

        public void solveA() {
            int t = in.nextInt();
            while (t-- > 0) {
                int n = in.nextInt();
                int x = in.nextInt();
                int a = in.nextInt();
                int b = in.nextInt();
                int minP = min(a, b);
                int maxP = max(a, b);
                int diffMin = minP - 1;
                int diffMax = n - maxP;

                if (diffMin <= x) {
                    minP = 1;
                    x -= diffMin;
                } else {
                    minP -= x;
                    x = 0;
                }

                if (diffMax <= x) {
                    maxP = n;
                    x -= diffMax;
                } else {
                    maxP += x;
                    x = 0;
                }

                out.println((maxP-minP));
            }
        }

        public void solveB() {
            int t = in.nextInt();
            while (t-- > 0) {
                long x = in.nextLong();
                long y = in.nextLong();
                if (x >= y) {
                    out.println("YES");
                } else {
                    checkForPossiblities(x, y);
                }
            }
        }

        private void checkForPossiblities(long a, long b) {
            int iter = 10000;
            Set<Long> set = new HashSet<>();
            while (iter-- > 0) {
                if (a == b) {
                    out.println("YES");
                    return;
                } else if (a > b) {
                    out.println("YES");
                    return;
                } else {
                    if (set.contains(a)) {
                        break;
                    }
                    set.add(a);
                    if (a % 2 == 0) {
                        a = (3 * a)/2;
                    } else {
                        a--;
                    }
                }
            }
            out.println("NO");
        }

        private void solveC() {
            int t = in.nextInt();

            while (t-- > 0) {
                int n = in.nextInt();
                int[] arr = in.nextIntArray(n);
                if (arr.length < 2) {
                    out.println(-1);
                } else {
                    HashMap<Integer, Integer> map = new HashMap<>();
                    int pos = 0;
                    int maxDiff = Integer.MAX_VALUE;
                    for (int item : arr) {
                        if (map.containsKey(item)) {
                            int diff = pos + 1 - map.get(item);
                            if (diff < maxDiff) {
                                maxDiff = diff;
                            }
                        }
                        map.put(item, pos);
                        pos++;
                    }
                    if (maxDiff == Integer.MAX_VALUE) {
                        out.println(-1);
                    } else {
                        out.println(maxDiff);
                    }
                }
            }
        }
    }

    private static long gcd(long a, long b) {
        if (a == 0) {
            return b;
        }
        return gcd(b % a, a);
    }

    private static long lcm(long a, long b) {
        return (a * b) / gcd(a, b);
    }

    private static int min(int a, int b) {
        return a < b ? a : b;
    }

    private static int max(int a, int b) {
        return a > b ? a : b;
    }

    private static int min(ArrayList<Integer> list) {
        int min = Integer.MAX_VALUE;
        for (int el : list) {
            if (el < min) {
                min = el;
            }
        }
        return min;
    }

    private static int max(ArrayList<Integer> list) {
        int max = Integer.MIN_VALUE;
        for (int el : list) {
            if (el > max) {
                max = el;
            }
        }
        return max;
    }

    private static int min(int[] list) {
        int min = Integer.MAX_VALUE;
        for (int el : list) {
            if (el < min) {
                min = el;
            }
        }
        return min;
    }

    private static int max(int[] list) {
        int max = Integer.MIN_VALUE;
        for (int el : list) {
            if (el > max) {
                max = el;
            }
        }
        return max;
    }

    private static void fill(int[] array, int value) {
        for (int i = 0; i < array.length; i++) {
            array[i] = value;
        }
    }


    static class FastReader {
        BufferedReader br;
        StringTokenizer st;

        public FastReader() {
            br = new BufferedReader(new
                    InputStreamReader(System.in));
        }

        String next() {
            while (st == null || !st.hasMoreElements()) {
                try {
                    st = new StringTokenizer(br.readLine());
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
            return st.nextToken();
        }

        int nextInt() {
            return Integer.parseInt(next());
        }

        long nextLong() {
            return Long.parseLong(next());
        }

        double nextDouble() {
            return Double.parseDouble(next());
        }

        String nextLine() {
            String str = "";
            try {
                str = br.readLine();
            } catch (IOException e) {
                e.printStackTrace();
            }
            return str;
        }

        int[] nextIntArray(int n) {
            int[] array = new int[n];
            for (int i = 0; i < n; i++) {
                array[i] = nextInt();
            }
            return array;
        }
    }
}