// No sorceries shall previal. //           
import java.util.Scanner; 
import java.io.PrintWriter;
import java.util.*;
import java.util.Arrays;
        
public class InVoker {       
    public static void main(String args[]) {
        		
        Scanner inp = new Scanner(System.in);
        PrintWriter out= new PrintWriter(System.out);
        	
        int t=inp.nextInt();
        while(t-->0) {
        	int n=inp.nextInt();
        	int a[]=new int[n];
        	for(int i=0;i<n;i++)
        		a[i]=inp.nextInt();
        	int freq[]=new int[n+1];
        	long ans=n+1;
        	for(int i=0;i<n;i++) {
        		if(freq[a[i]]==0) {
        			freq[a[i]]=i+1;
        		}
        		else {
        			ans=Math.min(ans, i+2-freq[a[i]]);
        			freq[a[i]]=i+1;
        		}
        	}
        	out.println(ans==n+1?-1:ans);
        }
        
        out.close();
        inp.close();					
    }
        		
}