
import java.lang.*;
import java.util.*;
import java.util.stream.Collectors;
import java.io.*;

public class absolute {
	static int[] I = new int[] {0,-1,0,1};
	static int[] J = new int[] {-1,0,1,0};
	static int[][] mat ;
	public static void main(String[] args) {
		// TODO Auto-generated method stub
		Scanner sc = new Scanner(System.in);
		int T = sc.nextInt();
		while(T-->0)
		{
			int n = sc.nextInt();
			int min = n+1;
			Map<Integer,List<Integer>> map = new HashMap<Integer,List<Integer>>();
			List<Integer> temp ;
			for(int i=0;i<n;i++)
			{
				int key = sc.nextInt();
				if(map.containsKey(key))
				{
					temp = map.get(key);
					min = Math.min(min, i-temp.get(temp.size()-1)+1);
					temp.add(i);
					map.replace(key, temp);
				}
				else
				{
					temp = new ArrayList<Integer>();
					temp.add(i);
					map.put(key, temp);
				}
			}
			
			if(min==n+1)System.out.println(-1);
			else System.out.println(min);
			
		}
		}
	
}