import java.util.Scanner;

public class Solution {
	
	public static void main(String[] args) {

		/*
		 * 
		 */
		Scanner scan = new Scanner(System.in);

		int t= scan.nextInt();
		while(t-->0) {
			int n = scan.nextInt();
			int[] arr = new int[n];
			
			for(int i=0;i<n;i++) {
				arr[i]=scan.nextInt();
			}
			
			int[] pos = new int[n+1];
			int ans=Integer.MAX_VALUE;
			for(int i=0;i<n;i++) {
				if(pos[arr[i]]!=0) {
					ans=Math.min(ans, i-pos[arr[i]]+2);
					if(ans==2) break;
				}
				pos[arr[i]]=i+1;
			}
			
			if(ans==Integer.MAX_VALUE) System.out.println(-1);
			else System.out.println(ans);
		}
		
		scan.close();
		
	}
	
}