

import java.util.ArrayList;
import java.util.Collection;
import java.util.Collections;
import java.util.Comparator;
import java.util.HashMap;
import java.util.HashSet;
import java.util.Iterator;
import java.util.LinkedList;
import java.util.List;
import java.util.Scanner;
import java.util.TreeMap;
import java.util.TreeSet;
import java.util.function.Function;
import java.util.stream.Collectors;

public class main {
   
	public static void main(String[] args) {
		Scanner s = new Scanner(System.in);
		int t = s.nextInt();
		while (t-- > 0) {
			int n = s.nextInt();
			int tab[] = new int[n];
			int occ[] = new int[n + 1];
			for (int i = 0; i < n; i++) {
				tab[i] = s.nextInt();
				
			}
			for (int i = 0; i <= n; i++) {
				occ[i] =-1;
				
			}
			int ans=n;
			Boolean bool=false;
			for (int i = 0; i < n; i++) {
				
				if(occ[tab[i]]!=-1) {
					ans=Math.min(i-occ[tab[i]]+1, ans);
					occ[tab[i]]=i;
					bool=true;
					
					
				}else {
					occ[tab[i]]=i;
				}
				
			}
			if(!bool) {System.out.println(-1); }else {
				System.out.println(ans);
			}
		
				
		

		}

	}
}