

import java.util.Scanner;

import static java.lang.Math.min;

public class C {
    public static void main(String[] args) {
        Scanner in = new Scanner(System.in);
        int t = in.nextInt();
        int inf = 1000_000_00;
        for (int i = 0; i < t; i++) {
            int mas[] = new int[200010];
            int n = in.nextInt();
            int a[] = new int[n];
            for (int j = 0; j < n; j++) {
                a[j] = in.nextInt();
            }
            if (n == 1) System.out.println(-1);
            else {
                int l = 0;
                int r = 0;
                mas[a[0]]++;
                int cnt = 0;
                int ans = inf;
                while (true) {
                    if (cnt == 0) {
                        while (r + 1 < n && cnt == 0) {
                            r++;
                            mas[a[r]]++;
                            if (mas[a[r]] > 1) cnt++;
                        }
                        if (cnt == 0) break;
                    } else {
                        while (cnt == 1) {
                            ans = min(ans, r - l);
                            mas[a[l]]--;
                            if (mas[a[l]] == 1) cnt = 0;
                            l++;
                        }
                    }
                }
                if (ans == inf) System.out.println(-1);
                else System.out.println(ans+1);
            }
        }
    }
}
