import java.util.Scanner;

public class Solution{
    public static void main(String[] args){
        Scanner in = new Scanner(System.in);

        int t = in.nextInt();
        for (int i = 0; i < t; i ++){
            int n = in.nextInt();
            int[] arr = new int[n];
            int[] foo = new int[n+1];
            for (int j = 0; j < n; j++){
                arr[j] = in.nextInt();
                foo[arr[j]]++;
            }
            Solution X = new Solution();
            X.getAnswer(n, arr, foo);
        }
    }

    private void getAnswer(int n, int[] array, int[] full){
        int a = 0, b = 0, c = 0;
        for (int i = 1; i <= n; i++){
            if (c <= full[i]){
                a = i;
                b = c;
                c = full[i];
            }
        }
        if (a == 0 || b == 0 || b == c){
            System.out.println(-1);
        }else{
            b = -200002;
            int res = 200002;
            for (int i = 0; i < n; i++){
                if (array[i] == a && i - b < res){
                    res = i - b;
                    b = i;
                }
            }
            System.out.println(++res);
        }
    }
}