import java.util.HashMap;
import java.util.Scanner;
// import java.util.HashSet;

public class test {
	public static void main(String[] args) {
		Scanner s = new Scanner(System.in);
		int t = s.nextInt();
		while(t-->0) {
			int n = s.nextInt();
			int arr[] = new int[n];
			for(int i=0;i<n;i++)
				arr[i] = s.nextInt();
			System.out.println(ans(arr, n));
		}		
	}
	private static int ans(int[] arr, int n) {
		if(n<2)
			return -1;
		int min = -1;
		HashMap<Integer, Integer> map = new HashMap<>();
		for(int i=0;i<n;i++) {
			if(map.containsKey(arr[i])) {
				int temp = map.get(arr[i]);
				int len = i-temp+1;
				if(min==-1 || min>len)
					min = len;
			}
			map.put(arr[i], i);
		}
		return min;
	}
}
