import java.util.*;
import java.io.*;
import java.lang.*;
public class Main{
     static class FastReader{
	BufferedReader br;
	StringTokenizer st;
      public FastReader(){
         br=new BufferedReader(new InputStreamReader(System.in));
         }
      public FastReader(String s)throws FileNotFoundException{
        br=new BufferedReader(new FileReader(new File(s)));
        }
       String next(){
          while(st==null || !st.hasMoreElements()){
            try{
                  st=new StringTokenizer(br.readLine());
                  }
            catch(IOException e){
                   e.printStackTrace();
		}

               }
             return st.nextToken();
         }
        String nextLine(){
             String str="";
             try{
                 str=br.readLine();
                 }
            catch(IOException e){
              e.printStackTrace();
                     }
                return str;
           }
	int nextInt(){
            return Integer.parseInt(next());
                }
         long nextLong(){
            return Long.parseLong(next());
             }
         double nextDouble(){
            return Double.parseDouble(next());

            }
  } 
    
	public static void main(String[] args)throws IOException {
	     FastReader in=new FastReader();
            int T=in.nextInt();
            while(T-->0){
              int n=in.nextInt();
              int a[]=new int[n+1];
               Arrays.fill(a,0);
               int ans=(int)(1e9);
               for(int i=1;i<=n;i++){
                   int x=in.nextInt();
                   if(a[x]!=0){
                      ans=Math.min(ans,i-a[x]+1);
                   } 
                  a[x]=i;
               }
               System.out.println(ans>n?-1:ans);  
            }
	     
       }
}		

