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

import static java.lang.Math.*;


public class Main {

    public static void main(String[] args) throws IOException {
        br = new BufferedReader(new InputStreamReader(System.in));
        PrintWriter out = new PrintWriter(System.out);
        int t = nextInt();
        for (int i = 0; i < t; i++) {
            int n = nextInt();
            int a[] = new int[n];
            for (int j = 0; j < n; j++) {
                a[j] = nextInt();
            }
            if (n == 1) {
                System.out.println(-1);
                continue;
            }
            int count[] = new int[n + 1];
            int l = -1;
            int r = 0;
            int ans = Integer.MAX_VALUE;
            int cnt = 1;
            int max = a[r];
            while (l < n - 1) {
                l++;
                if (l <= 0 || max == a[l - 1]) {
                    if (l - 1 != -1 && max == a[l - 1]) cnt--;
                    boolean p = false;
                    int last = cnt;
                    for (; r < n; r++) {
                        count[a[r]]++;
                        p = true;
                        if (count[a[r]] > cnt && r != 0) {
                            cnt = count[a[r]];
                            max = a[r];
                            r++;
                            break;
                        }
                    }
                    if (last != cnt - 1) max = a[l];
                    if (cnt - 1 == last && p && r - l < ans) ans = r - l;
                } else if (r - l < ans) ans = r - l;
                if (r != 0) {
                    count[a[l]]--;
                }
            }
            System.out.println(ans);
        }
    }


    static StringTokenizer st = new StringTokenizer("");
    static BufferedReader br;

    static String next() throws IOException {
        while (st == null || !st.hasMoreTokens()) st = new StringTokenizer(br.readLine());
        return st.nextToken();
    }

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


