//  package cp;
import java.io.*;
import java.math.*;
import java.util.*;
public class Solution{
	long gcd(long a,long b) {if(b==0)return a;else return gcd(b,a%b);}
	void swap(long a,long b) {long temp=a;a=b;b=temp;}
	StringBuilder sb=new StringBuilder();
	Integer[] ARR=new Integer[5];
	//Integer sort-TLE-Initialize object elements i.e a[0].
	long ceil(int n) {return (long)Math.ceil(1.0*n/2);}
	void divisor(long n,int start) {
		int cnt=0;for(int i=start;i<=Math.sqrt(n);i++) {
		if(n%i==0) {if(i==n/i)cnt++;else cnt+=2;}}}
	
	public static void main(String[] args) throws IOException {	
		PrintWriter out = new PrintWriter(System.out);
		Readers.init(System.in);
		Solution obj=new Solution();
		int t=Readers.nextInt();
		while(t-->0) {
			int n=Readers.nextInt();
			int[] a=new int[n];
			HashMap<Integer,Integer> map=new HashMap<>();
			for (int i = 0; i <n; i++) {
				a[i]=Readers.nextInt();
			}
			int minn=Integer.MAX_VALUE;
			int flag=0;
			for (int i = 0; i < a.length; i++) {
				if(map.containsKey(a[i])) {
					flag=1;
					minn=Math.min(minn, i-map.get(a[i]));
				}
				map.put(a[i],i);
			}
			if(flag==0)System.out.println(-1);
			else System.out.println(minn+1);
		}
		out.flush();
	}
}
	
class Readers {
 
    static BufferedReader reader;
 
    static StringTokenizer tokenizer;
 
 
 
    /** call this method to initialize reader for InputStream */
 
    static void init(InputStream input) {
 
        reader = new BufferedReader(
 
                     new InputStreamReader(input) );
 
        tokenizer = new StringTokenizer("");
 
    }
 
 
 
    /** get next word */
 
    static String next() throws IOException {
 
        while ( ! tokenizer.hasMoreTokens() ) {
 
            //TODO add check for eof if necessary
 
            tokenizer = new StringTokenizer(
 
                   reader.readLine() );
 
        }
 
        return tokenizer.nextToken();
 
    }
 
 
 
    static int nextInt() throws IOException {
 
        return Integer.parseInt( next() );
 
    }
 
	
 
    static double nextDouble() throws IOException {
 
        return Double.parseDouble( next() );
 
    }
    
    
    static long nextLong() throws IOException{
    	
		return Long.parseLong(next());
	}
 
}