import java.util.*; 
import java.lang.*;
import java.io.*;
public class Main 
{ 
    static class FastReader 
    { 
        BufferedReader br; 
        StringTokenizer st; 
  
        public FastReader() 
        { 
            br = new BufferedReader(new
                     InputStreamReader(System.in)); 
        } 
  
        String next() 
        { 
            while (st == null || !st.hasMoreElements()) 
            { 
                try
                { 
                    st = new StringTokenizer(br.readLine()); 
                } 
                catch (IOException  e) 
                { 
                    e.printStackTrace(); 
                } 
            } 
            return st.nextToken(); 
        } 
  
        int nextInt() 
        { 
            return Integer.parseInt(next()); 
        } 
  
        long nextLong() 
        { 
            return Long.parseLong(next()); 
        } 
  
        double nextDouble() 
        { 
            return Double.parseDouble(next()); 
        } 
  
        String nextLine() 
        { 
            String str = ""; 
            try
            { 
                str = br.readLine(); 
            } 
            catch (IOException e) 
            { 
                e.printStackTrace(); 
            } 
            return str; 
        } 
    } 
    static FastReader s=new FastReader();
    public static void solve(){
        int n;
        // int m;
        n = s.nextInt();
        // m = s.nextInt();
        int a[] = new int[n];
        int b[] = new int[n];
        for(int i=0;i<n;i++){
            a[i]=s.nextInt()-1;
            b[i]=-1;
        }
        int mn = n+1;
        for(int i=0;i<n;i++){
            int len = 0;
            if(b[a[i]]==-1){
                b[a[i]]=i;
            }
            else{
                len = i-b[a[i]]+1;
                b[a[i]]=i;
                mn = Math.min(mn,len);
            }
        }
        if(mn==n+1){
            System.out.println(-1);
        }
        else{
            System.out.println(mn);
        }
        return;
    }
    public static void main(String[] args) 
    {  
        // 1279 C -> Stack of Presents
        int T = s.nextInt();
        while(T>0){
            solve();
            T--;
        }
    } 
}