/*
Code for task B by detestmaths
 */


import java.io.*;
import java.time.Year;
import java.util.*;

import static java.lang.Math.*;
import static java.util.Arrays.*;
import static java.lang.System.*;

public class Solver {
    public static void main(String[] args) throws IOException {
        FastScanner in = new FastScanner(System.in);
        PrintWriter out = new PrintWriter(System.out);
        int t = in.nextInt();
        while (t-- > 0) {
            int n = in.nextInt();
            int a[] = new int[n];
            for (int i = 0; i < n; i++) {
                a[i] = in.nextInt();
            }
            TreeMap<Integer,Integer>id = new TreeMap<>();
            int ans = Integer.MAX_VALUE;
            for (int i = 0; i < n; i++) {
                if(id.containsKey(a[i])){
                    int idd = id.get(a[i]);
                    ans = min(ans,i - idd + 1);
                    id.put(a[i],i);
                }else{
                    id.put(a[i],i);
                }
            }
            if(ans == Integer.MAX_VALUE)ans = -1;
            out.println(ans);
        }
        out.close();
    }
}

class FastScanner {
    BufferedReader br;
    StringTokenizer st = new StringTokenizer("");

    public FastScanner(InputStream s) {
        br = new BufferedReader(new InputStreamReader(s));
    }

    public FastScanner(String s) throws FileNotFoundException {
        br = new BufferedReader(new FileReader(new File(s)));
    }

    public String next() throws IOException {
        if (!st.hasMoreTokens())
            st = new StringTokenizer(br.readLine());
        return st.nextToken();
    }

    public int nextInt() throws IOException {
        return Integer.parseInt(next());
    }

    public long nextLong() throws IOException {
        return Long.parseLong(next());
    }

    public double nextDouble() throws IOException {
        return Double.parseDouble(next());
    }
}