import java.util.*;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.StringTokenizer;

public class a{ 
     static int[] count;
     // static int[] count1;
     static int[] arr;
     static char[] ch,c1;
     static long[] darr;
     static long x;
     static long maxl;
     static double dec;
     static String s;
     // static long minl;
     static int mx = (int)1e6;
     static long mod = 998244353l;
     // static int minl = -1;
     // static long n;
     static int n,n1,n2;
     static long a;
     static long b;
     static long c;
     static long d;
     static long y;
     static long m;
     static long k;
     static int q;
     static String[] str,str1;
     static Set<Character> set,set1;
     static List<Long> list;
     static Map<Long,Integer> map;
     static StringBuilder sb;

     public static void solve(){

        count = new int[n+1];

        for(int i = 0 ; i <= n ; i++)
            count[i] = -1;

        int minl = n+1;

        for(int i = 0 ; i < n ; i++){
            if(count[arr[i]] != -1){
                minl = Math.min(minl,i-count[arr[i]]+1);
            }
            count[arr[i]] = i;
        }
        if(minl == n+1)
            System.out.println("-1");
        else
            System.out.println(minl);

     }

     public static void main(String[] args) {

            // FastScanner sc = new FastScanner();
            Scanner sc = new Scanner(System.in);
            int t = sc.nextInt();
            while(t > 0){
                 // set = new HashSet<>();
                 // map = new HashMap<>();
                
                // x = sc.nextLong();
                // y = sc.nextLong();
                // a = sc.nextLong();
                // b = sc.nextLong();
                
                // c = sc.nextLong();
                // d = sc.nextLong();
                // n = sc.nextInt();
                // x = sc.nextLong();
                // ch = sc.next().toCharArray();
                // s = sc.next();

                 // long x = sc.nextLong();

                
                // k = sc.nextLong();
                // dec = sc.nextDouble();
                n = sc.nextInt();
                // m = sc.nextLong();
                 
                arr = new int[n];
                for(int i = 0; i < n ; i++){
                    arr[i] = sc.nextInt();
                }
                // set = new HashSet<>();
                // for(int i = 0 ; i < n ; i++)
                //     set.add(sc.next().charAt(0));
                

                 // darr = new long[n2];
                 // for(int i = 0; i < n2 ; i++){
                 //     darr[i] = sc.nextLong();
                 // }



                // System.out.println(solve()?"red":"cyan");
                solve();
                 // System.out.println(solve());
                 // sb = new StringBuilder();
                 // sb.append(str);

                 t -= 1;
            }

     }

    public static int log(long n){
         if(n == 0)
             return 0;

         if(n == 1)
             return 0;
         if(n == 2)
             return 1;
         double num = Math.log(n);
         double den = Math.log(2);
         if(den == 0)
             return 0;
         return (int)(num/den);
     }

     // public static void swap(int i,int j){
     //     long temp = arr[j];
     //     arr[j] = arr[i];
     //     arr[i] = temp;
     // } 

    static final Random random=new Random();
    
    static void ruffleSort(long[] a) {
        int n=a.length;//shuffle, then sort 
        for (int i=0; i<n; i++) {
            int oi=random.nextInt(n);
            long temp=a[oi];
            a[oi]=a[i]; a[i]=temp;
        }
        Arrays.sort(a);
    }

    static class Node{
        int first;
        int second;
        Node(int f,int s){
            this.first = f;
            this.second = s;
        }
    }

     static class FastScanner {

                BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
                StringTokenizer st=new StringTokenizer("");
                String next() {
                        while (!st.hasMoreTokens())
                                try {
                                        st=new StringTokenizer(br.readLine());
                                } catch (IOException e) {
                                        e.printStackTrace();
                                }
                        return st.nextToken();
                }
                
                int nextInt() {
                        return Integer.parseInt(next());
                }
                int[] readArray(int n) {
                        int[] a=new int[n];
                        for (int i=0; i<n; i++) a[i]=nextInt();
                        return a;
                }
                long nextLong() {
                        return Long.parseLong(next());
                }
        }

}