import java.util.Scanner;

/**
 * Created by codancer on 2019.11.14.
 **/
public class TaskC {
    static int [] pos = new int [300000];
    public static void main(String [] args){
        int T,n,x;
        Scanner cin = new Scanner(System.in);
        T=cin.nextInt();
        while(T-->0){
            n=cin.nextInt();
            for(int i=1;i<=n;i++) pos[i]=-1;
            int ans=100000000;
            for(int i=1;i<=n;i++){
                x=cin.nextInt();
                if(pos[x]!=-1){
                    ans=Math.min(ans,i-pos[x]+1);
                }
                pos[x]=i;
            }
            if(ans==100000000) ans=-1;
            System.out.println(ans);
        }
    }
}
