import java.util.*;

public class donimatedSubArray {
	public static void main(String[] args) {
		Scanner in = new Scanner(System.in);
		int t = in.nextInt();
		
		for(int i = 0; i < t; i++) {
			int n = in.nextInt();
			int min = Integer.MAX_VALUE;
			HashMap<Integer, Integer> map = new HashMap<>();
			
			for(int j = 0; j < n; j++) {
				int k = in.nextInt();
				if(!map.containsKey(k)) map.put(k, j);
				else {
					min = Math.min(min, j - map.get(k));
					map.put(k, j);
				}
			}
			
			System.out.println((min != Integer.MAX_VALUE) ? min+1 : -1);
		}
		
		in.close();
	}
}