import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.io.IOException;
import java.util.StringTokenizer;
import java.io.PrintWriter;
import java.util.Arrays;
public class Main{
    public static void main(String[] args)throws IOException{
	br = new BufferedReader(new InputStreamReader(System.in));
	PrintWriter out = new PrintWriter(System.out);
	int c = nextInt();
	while(c-->0){
	    int n = nextInt();
	    int cnt[] = new int[n+1];
	    Arrays.fill(cnt,-1);
	    int ans = n+1, x = 0;
	    for(int i = 0; i<n;i++  ){
		x = nextInt();
		if(cnt[x]!=-1){
		    ans = Math.min(ans,i-cnt[x]);
		}
		cnt[x] = i;
	    }
	    out.println(ans==(n+1)?-1:ans+1);
	}
	out.close();
    }

    static BufferedReader br;
    static StringTokenizer st = new StringTokenizer("");
    static String next()throws IOException{
	while(!st.hasMoreTokens()){
	    st = new StringTokenizer(br.readLine());
	}
	return st.nextToken();
    }
    static int nextInt()throws IOException{
	return Integer.parseInt(next());
    }
}
