import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.ArrayList;
import java.util.Hashtable;

public class DominatedSubarray {
	
	static int minDominatedLength(int [] a) {
		Hashtable<Integer, Integer> occurences = new Hashtable<>();
		Hashtable<Integer, ArrayList<Integer>> dominance_points = new Hashtable<>();
		ArrayList<Integer> points;
		
		int max_occ = Integer.MIN_VALUE;
		int min_dom = Integer.MAX_VALUE;
		int occ, size;
		boolean points_exist = false;
		
		for (int i = 0; i < a.length; i++) {
			if(!occurences.containsKey(a[i])) {
				occurences.put(a[i], 0);
			}
			
			occ = occurences.get(a[i]) + 1;
			occurences.put(a[i], occ);
			
			if(occ > max_occ) {
				max_occ = occ;
				
				if(i > 0) {	
					points_exist = true;
					
					if(!dominance_points.containsKey(a[i])) {
						dominance_points.put(a[i], new ArrayList<>());
					}
					
					points = dominance_points.get(a[i]);
					points.add(i+1);
					dominance_points.put(a[i], points);
				}
			}
		}

		//System.out.println(dominance_points);
		
		for (int key : dominance_points.keySet()) {
			points = dominance_points.get(key);
			size = points.size();
			
			for (int i = 0; i < size; i++) {
				min_dom = Math.min(min_dom, points.get(i));
				
				if(i+1 < size) {
					min_dom = Math.min(min_dom, (points.get(i+1) - points.get(i) + 1));
				}
			}
		}
		
		if(!points_exist) {
			min_dom = -1;
		}
		
		return min_dom;
	}
	
	public static void main(String[] args) throws IOException {
		BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
		int test_cases = Integer.parseInt(br.readLine());
		String [] numbers_str;
		int [] numbers;
		int numbers_count;
		
		for (int i = 0; i < test_cases; i++) {
			numbers_count = Integer.parseInt(br.readLine());
			numbers_str = (br.readLine()).split(" ");
			numbers = new int [numbers_count];
			
			for (int j = 0; j < numbers_count; j++) {
				numbers[j] = Integer.parseInt(numbers_str[j]);
			}
			
			System.out.println(minDominatedLength(numbers));
		}
	}

}