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

public class DominatedSubarray {
	
	static int minDominated(int [] a) {
		Hashtable<Integer, Integer> last_occurence = new Hashtable<>();
		int min_dom = Integer.MAX_VALUE;
		boolean answer_exist = false;
		
		for (int i = 0; i < a.length; i++) {
			if(last_occurence.containsKey(a[i])) {
				answer_exist = true;
				min_dom = Math.min(min_dom, i-last_occurence.get(a[i])+1);
			}
			
			last_occurence.put(a[i], i);
		}
		
		return answer_exist ? min_dom : -1;
	}

	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(minDominated(numbers));
		}
	}

}