import java.io.*;
import java.util.*;

public class DominatedSubarray{
	public static void main(String[] args) throws IOException{
		BufferedReader br = new BufferedReader(new InputStreamReader(System.in));

		int t = Integer.parseInt(br.readLine());

		String st[];

		while(t-- != 0){
			int len = Integer.parseInt(br.readLine());
			
			if(len <= 1){
				System.out.println(-1);
				br.readLine();
				continue;
			}

			st = br.readLine().split(" ");

			int arr[] = new int[len];
			int i;

			for(i = 0; i < len; i++){
				arr[i] = Integer.parseInt(st[i]);
			}

			int m, n;

			int bit[] = new int[len+1];
			bit[arr[0]] = 1;

			m = 0;

			int smallestSubarray = Integer.MAX_VALUE;

			for(n = 1; n < len; n++){
				if(bit[arr[n]] == 1){
					while(arr[m] != arr[n]){
						bit[arr[m++]] = 0;
					}
					if(arr[m] == arr[n]){
						if(smallestSubarray > n-m+1){
							smallestSubarray = n-m+1;
							m++;
						}
					}
				}
				else{
					bit[arr[n]] = 1;
				}
			}

			if(smallestSubarray == Integer.MAX_VALUE){
				System.out.println(-1);
			}
			else{
				System.out.println(smallestSubarray);
			}
		}
	}
}