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

public class DominatedSubarray {
    public static void main(String[] args) {
        FastScanner scanner = new FastScanner();
        PrintWriter out = new PrintWriter(System.out,false);
        int t = scanner.nextInt();
        while(t-->0) {
            int n= scanner.nextInt();
            int[] arr = new int[n];
            for(int i = 0; i < n; i++) arr[i] = scanner.nextInt()-1;
            int[] prev = new int[n];
            Arrays.fill(prev, -1);
            int min = Integer.MAX_VALUE;
            for(int i= 0; i < n; i++) {
                if (prev[arr[i]] != -1) { min = Math.min(i - prev[arr[i]] + 1, min); }
                prev[arr[i]] = i;
            }
            if (min == Integer.MAX_VALUE) out.println(-1);
            else out.println(min);
        }
        out.flush();
    }
    public static class FastScanner {
        BufferedReader br;
        StringTokenizer st;
        public FastScanner(Reader in) {
            br = new BufferedReader(in);
        }
        public FastScanner() {
            this(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 readNextLine() {
            String str = "";
            try {
                str = br.readLine();
            } catch (IOException e) {
                e.printStackTrace();
            }
            return str;
        }
    }
}
