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

public class Main {
	
	public static void main(String[] args) throws IOException {
		
		BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
		
		int T = Integer.parseInt(br.readLine());
		
		for(int i = 0; i < T; ++i) {
			
			int n = Integer.parseInt(br.readLine());
			String[] numsS = br.readLine().split(" ");
			
			int[] num = new int[n];
			for(int j = 0; j < n; ++j) {
				num[j] = Integer.parseInt(numsS[j]);
			}
			
			Hashtable<Integer, Integer> dic = new Hashtable<Integer, Integer>(); 
			
			int shortest = Integer.MAX_VALUE;
			dic.put(num[0], 0);	
			
			for(int j = 1; j < n; ++j) {
				if(dic.containsKey(num[j])) 
					shortest = Math.min(shortest, j - dic.get(num[j])); 
				
				dic.put(num[j],j);
			}
			
			if(shortest != Integer.MAX_VALUE) {
				System.out.println(shortest+1);
			}else {
				System.out.println(-1);
			}
			
		}
		
	}

}
