import java.io.OutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.PrintWriter;
import java.util.Arrays;
import java.util.StringTokenizer;
import java.io.IOException;
import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.io.InputStream;

/**
 * Built using CHelper plug-in
 * Actual solution is at the top
 *
 * @author AMR
 */
public class Main {
    public static void main(String[] args) {
        InputStream inputStream = System.in;
        OutputStream outputStream = System.out;
        FastReader in = new FastReader(inputStream);
        PrintWriter out = new PrintWriter(outputStream);
        TaskC solver = new TaskC();
        solver.solve(1, in, out);
        out.close();
    }

    static class TaskC {
        public void solve(int testNumber, FastReader in, PrintWriter out) {
            int T = in.nextInt();
            for (int k = 0; k < T; k++) {
                int n = in.nextInt();
                int[][] arr = new int[n][2];
                for (int i = 0; i < n; i++) {
                    arr[i][0] = in.nextInt();
                    arr[i][1] = i;
                }
                Arrays.sort(arr, (a, b) -> Integer.compare(a[0], b[0]));
                int minDist = Integer.MAX_VALUE;
                for (int i = 0; i < n - 1; i++) {
                    if (arr[i + 1][0] == arr[i][0])
                        minDist = Math.min(minDist, arr[i + 1][1] - arr[i][1] + 1);
                }
                if (minDist == Integer.MAX_VALUE) out.println(-1);
                else out.println(minDist);
            }
        }

    }

    static class FastReader {
        BufferedReader br;
        StringTokenizer st;

        public FastReader(InputStream inputStream) {
            br = new BufferedReader(new InputStreamReader(inputStream));
            st = null;
        }

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

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

    }
}

