
import java.util.*;
import java.io.*;
public class Codeforces6 {

	public static void main(String[] args) throws IOException{
		// TODO Auto-generated method stub
		BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
		int t=Integer.parseInt(br.readLine());
		while(t--!=0) {
			int n=Integer.parseInt(br.readLine());
			String s[]=br.readLine().split(" ");
			int a[]=new int[n];
			for(int i=0;i<n;i++) a[i]=Integer.parseInt(s[i]);
			HashSet<Integer> hs=new HashSet<>();
			HashMap<Integer,Integer> hm=new HashMap<>();
			int min= (int) (1e5 + 958);
			int count;
			boolean flag=false;
			for(int i=0;i<n;i++) {
				if(hm.containsKey(a[i])) {
					min=Math.min(min, (Math.abs(hm.get(a[i])-i)+1));
					hm.replace(a[i], i);
					flag=true;
				}
				else hm.put(a[i],i);
			}
			if(flag)System.out.println(min);
			else System.out.println("-1");
		}
	}
}
