import java.util.*;
import java.io.*;

public class Main {
    static class FastReader {
        BufferedReader br;
        StringTokenizer st;

        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;
        }
    }

    public static void main(String[] args) throws IOException {
          FastReader ip = new FastReader();
          OutputStream output = System.out;
          PrintWriter out = new PrintWriter(output);
          int t=ip.nextInt();
          while(t-->0){
             int n=ip.nextInt();
             int arr[]=new int[n];
             for(int i=0;i<n;i++){
                 arr[i]=ip.nextInt();
             }
             HashMap<Integer,Integer> hm=new HashMap<>();
             int ans=Integer.MAX_VALUE;
             for(int i=0;i<n;i++){
                 if(hm.containsKey(arr[i])){
                     int pos=hm.get(arr[i]);
                     ans=Math.min(i-pos,ans);
                     hm.put(arr[i],i);
                 }else{
                     hm.put(arr[i],i);
                 }
             }
             int check=0;
             for(int i=0;i<n-1;i++){
                 if(arr[i]!=arr[i+1]){
                     check=1;
                     break;
                 }
             }

             if(check==0 && n<2){
                 out.println(-1);
             }else if(ans==Integer.MAX_VALUE){
                 out.println(-1);
             }else if(ans==1 && check==0){
                 out.println(2);
             }else{
                 out.println(ans+1);
             }



          }

        out.close();
    }
}