import java.util.ArrayList;
import java.util.Scanner;

public class Main {


    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        int testcase =sc.nextInt();
        while(testcase-->0){
            int n = sc.nextInt();
            int st[] = new int[(int)1e6];
            int inp[] = new int[n];
            for(int i=0; i<n; i++){
                inp[i] = sc.nextInt();
            }
            int ans = (int) 1e7;
            for(int i=0;i<n; i++){
                    if (st[inp[i]] != 0) {
                        ans = Math.min(ans,i+2- st[inp[i]]);
                    }
                    st[inp[i]]=i+1;
            }
            if(ans!=1e7){
                System.out.println(ans);
            }else{
                System.out.println(-1);
            }
        }

    }
}

