import java.util.Scanner;

public class Main {

    public static void main(String[] args)
    {
        int t , n ; int[] a;
        int occ , focc , min , fmin , var = -1;
        boolean bool = true;

        Scanner sc=new Scanner(System.in);

        t = sc.nextInt();
        for (int z = 0 ; z < t ; z++){
            n = sc.nextInt();
            a = new int[n];
            for (int i = 0 ; i < n ; i++){
                a[i] = sc.nextInt();
            }
            if (n < 2) System.out.println("-1");
            else{
                occ = 0 ; focc = 0 ; min = n ; fmin = n;
                for (int i = 0 ; i < n ; i++){
                    for (int j = 0 ; j < n ; j++){
                        if (a[i] == a[j]) focc++;
                    }
                    if (focc > occ) {
                        occ = focc;
                        var = a[i];}
                    focc = 0;
                }
                for (int i = 0 ; i < n ; i++){
                    if (var!=a[i] && bool){
                        for (int j = 0 ; j < n ; j++){
                            if (a[i] == a[j]) focc++;
                        }
                        if (focc == occ) {
                            System.out.println("-1");
                            bool = false;
                        }
                        focc = 0;
                    }
                }
                if (bool) {
                    for (int i = 0; i < n; i++) {
                        if (var==a[i]){
                            for(int j = i ; j<n ; j++){
                                if (i!=j && var==a[j]){
                                    fmin = j-i;
                                    break;
                                }
                            }
                        }
                        if (fmin < min) min = fmin;
                    }
                    System.out.println(min+1);
                }
            }
        }
    }
}
