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

	public static void main(String[] args) throws Exception{
		int num = 998244353;

		// TODO Auto-generated method stub
 		BufferedReader bf = new BufferedReader(new InputStreamReader(System.in));
 		PrintWriter out = new PrintWriter(System.out);
 		int t = Integer.parseInt(bf.readLine());
 		for(int i = 0;i<t;i++){
//	 		String input1 = bf.readLine().trim();
//	 		String input2 = bf.readLine().trim();
	 		int n = Integer.parseInt(bf.readLine());
	 		ArrayList<Integer> array = new ArrayList<Integer>();
	 		StringTokenizer st2 = new StringTokenizer(bf.readLine());
	 		for(int j=0;j<n;j++){
	 			array.add(Integer.parseInt(st2.nextToken()));
	 		}
	 		Map<Integer, Integer> mp = new HashMap<Integer, Integer>();
	 		mp.put(array.get(0),  0);
	 		int min = Integer.MAX_VALUE;
	 		for(int j =1;j<n;j++){
	 			if (!mp.containsKey(array.get(j))){
	 				mp.put(array.get(j), j);
	 			}
	 			else{
	 				min = Math.min(j-mp.get(array.get(j)), min);
	 				mp.put(array.get(j),  j);
	 			}
	 		}
	 		if (min == Integer.MAX_VALUE)
	 			out.println(-1);
	 		else
	 			out.println(min+1);
 		}
 		
 		
 		out.println();
	 		
 		out.close();
 		
 		
 		
 	}
}
 	
 
//StringJoiner sj = new StringJoiner(" "); 
//sj.add(strings)
//sj.toString() gives string of those stuff w spaces or whatever that sequence is

 		
 		
 		
 		
	


