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

public class Abc {
    public static void main(String[] args) throws IOException {
        FastReader sc = new FastReader();
        int t=sc.nextInt();
        while (t-->0){
            int n=sc.nextInt();
            int arr[]=new int[n+1];
            for (int i=1;i<=n;i++)arr[i]=sc.nextInt();
            int tmp[]=new int[n+1];
            int l=1;
            int min=Integer.MAX_VALUE;
            while (l<=n){
                if (tmp[arr[l]]!=0){
                    min=Math.min(l-tmp[arr[l]]+1,min);
                    tmp[arr[l]] = l;
                }else {
                    tmp[arr[l]] = l;
                }
                l++;
            }
            if (min==Integer.MAX_VALUE) System.out.println(-1);
            else System.out.println(min);

        }
    }

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