import java.io.InputStreamReader;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.Map.Entry;
import java.util.Scanner;
import java.util.Set;


public class Main {
	public static void main(String[] args) {
		Scanner in=new Scanner(System.in);
		int t=in.nextInt();
		while(t--!=0){
			int n=in.nextInt();
			int arr[]=new int[n+1];
			int ans=Integer.MAX_VALUE;
			int lne=(int) 2e5+5;
//			System.out.println(lne);
			int pre[]=new int[lne];
			for(int i=1;i<=n;i++){
				arr[i]=in.nextInt();
				if(pre[arr[i]]==0){
					pre[arr[i]]=i;
				}else{
					ans=ans>(i-pre[arr[i]])?i-pre[arr[i]]:ans;
					pre[arr[i]]=i;
				}
			}
			if(ans!=Integer.MAX_VALUE){
				System.out.println(ans+1);
			}else{
				System.out.println(-1);
			}
		}
	}
}
