import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.HashMap;
import java.util.StringTokenizer;

public class C_DomArray {
	public static void main(String[] args) throws NumberFormatException, IOException {
		BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
		int T = Integer.parseInt(br.readLine());
		
		for(int t = 0; t < T; t++) {
			int n = Integer.parseInt(br.readLine());
			StringTokenizer st = new StringTokenizer(br.readLine());
			int[] arr = new int[n];
			
			HashMap<Integer, Integer> hm = new HashMap<>();
			Integer occ = 0;
			int min = Integer.MAX_VALUE;
			
			for(int i = 0; i < n; i++) {
				arr[i] = Integer.parseInt(st.nextToken());
				occ = hm.get(arr[i]);
				hm.put(arr[i], i);
				if(occ == null) continue;
				else {
					int length = i - occ + 1;
					if(min > length) min = length;
				}
				if(min == 2) break;
			}
			
			if(min == Integer.MAX_VALUE) min = -1;
			System.out.println(min);
			
		}
	}
}
