import java.io.OutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.PrintWriter;
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 htvu
 */
public class Main {
    public static void main(String[] args) {
        InputStream inputStream = System.in;
        OutputStream outputStream = System.out;
        InputReader in = new InputReader(inputStream);
        PrintWriter out = new PrintWriter(outputStream);
        CDominatedSubarray solver = new CDominatedSubarray();
        solver.solve(1, in, out);
        out.close();
    }

    static class CDominatedSubarray {
        public void solve(int testNumber, InputReader in, PrintWriter out) {
            int T = in.nextInt();
            while (T-- > 0) {
                int n = in.nextInt();
                int[] a = new int[n];
                for (int i = 0; i < n; ++i)
                    a[i] = in.nextInt();
                int L = 0, maxOcc = 0, count = 0;
                int[] occ = new int[n + 1];
                int ans = Integer.MAX_VALUE;
                for (int R = 0; R < n; ++R) {
                    occ[a[R]]++;
                    if (occ[a[R]] > maxOcc) {
                        maxOcc = occ[a[R]];
                        count = 1;
                    } else if (occ[a[R]] == maxOcc) {
                        count++;
                    }

//                out.println(L + " " + R + " " + maxOcc + " " + count);
                    while (L < n && maxOcc > 1 && count >= 1) {
                        if (count == 1 && R > L)
                            ans = Math.min(ans, R - L + 1);

                        if (occ[a[L]] == maxOcc) {
                            count--;
                        }
                        if (count == 0) maxOcc--;
                        occ[a[L++]]--;

                    }

                }
                out.println(ans == Integer.MAX_VALUE ? -1 : ans);

            }
        }

    }

    static class InputReader {
        public BufferedReader reader;
        public StringTokenizer tokenizer;

        public InputReader(InputStream stream) {
            reader = new BufferedReader(new InputStreamReader(stream), 32768);
            tokenizer = null;
        }

        public String next() {
            while (tokenizer == null || !tokenizer.hasMoreTokens()) {
                try {
                    tokenizer = new StringTokenizer(reader.readLine());
                } catch (IOException e) {
                    throw new RuntimeException(e);
                }
            }
            return tokenizer.nextToken();
        }

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

    }
}

