
import java.io.IOException;
import java.io.InputStream;
import java.io.PrintWriter;
import java.util.*;

public class q10 {
    InputStream is;
    PrintWriter out;

    class diff{
        int occ;
        int pos;
    }

    void solve()
    {
        int t = ni();
        while(t-->0){
            int n = ni();
            int arr[] = na(n);
            HashMap<Integer,diff> map = new HashMap<>();

            for(int i =0;i<n;i++){
                if(map.containsKey(arr[i])){
                diff dff = map.get(arr[i]);
                int v1 = dff.occ;
                int v2 = dff.pos;
                v1++;
                v2=i;
                dff.occ=v1;
                dff.pos=v2;
                map.remove(arr[i]);
                map.put(arr[i],dff);
                }
                else {
                    diff dff = new diff();
                    dff.occ = 1;
                    dff.pos = i;
                    map.put(arr[i],dff);
                }
            }
            int min = Integer.MAX_VALUE;
            HashMap<Integer,Boolean> map1 = new HashMap<>();

            for(int i=0;i<n;i++){
                diff dff = map.get(arr[i]);
                int curPos = dff.pos;
                int diffpos = Math.abs(curPos-i);
                if(diffpos<min && diffpos!=0){
                    if(map1.containsKey(diffpos)){
                        map1.remove(diffpos);
                        map1.put(diffpos,false);
                    }
                    else {
                        map1.put(diffpos,true);
                    }
                }
            }

            Set<Integer> s = map1.keySet();
            ArrayList<Integer> list = new ArrayList<>();

            for (int item: s) {
                if(map1.get(item)){
                    list.add(item);
                }
            }
            int len = list.size();
            for(int i=0;i<len;i++){
                if(list.get(i)<min){
                    min = list.get(i);
                }
            }
            if(min!=Integer.MAX_VALUE){
                out.println(min+1);
            }
            else {
                out.println(-1);
            }

        }
    }


    //---------- I/O Template ----------

    public static void main(String[] args) { new q10().run(); }
    long nCr(int n, int r)
    {
        return fact(n) / (fact(r) *
                fact(n - r));
    }

    long fact(int n)
    {
        long res = 1;
        for (int i = 2; i <= n; i++)
            res = res * i;
        return res;
    }

    void run() {
        is = System.in;
        out = new PrintWriter(System.out);
        solve();
        out.flush();
    }

    byte input[] = new byte[1024];
    int len = 0, ptr = 0;

    int readByte() {
        if(ptr >= len) { ptr = 0;
            try { len = is.read(input); }
            catch(IOException e) { throw new InputMismatchException(); }
            if(len <= 0) { return -1; }
        } return input[ptr++];
    }
    boolean isSpaceChar(int c) { return !( c >= 33 && c <= 126 ); }
    int skip() {
        int b = readByte();
        while(b != -1 && isSpaceChar(b)) { b = readByte(); }
        return b;
    }

    char nc() { return (char)skip(); }
    String ns() {
        int b = skip();
        StringBuilder sb = new StringBuilder();
        while(!isSpaceChar(b)) { sb.appendCodePoint(b); b = readByte(); }
        return sb.toString();
    }

    int[] fib(int n ){
        int arr[] = new int[n];
        arr[1]=0;
        arr[2]=1;

        for(int i=3;i<=n;i++){
            arr[i] =(arr[i-2]+arr[i-1]);
//
//
        }

        return arr;
    }


    int findlastdig(long n,int f[]){

        int index = (int)(n%60L);

        return f[index];
    }

    String nLine() {
        int b = skip();
        StringBuilder sb = new StringBuilder();
        while( !(isSpaceChar(b) && b != ' ') ) { sb.appendCodePoint(b); b = readByte(); }
        return sb.toString();
    }
    int ni() {
        int n = 0, b = readByte();
        boolean minus = false;
        while(b != -1 && !( (b >= '0' && b <= '9') || b == '-')) { b = readByte(); }
        if(b == '-') { minus = true; b = readByte(); }
        if(b == -1) { return -1; }  //no input
        while(b >= '0' && b <= '9') { n = n * 10 + (b - '0'); b = readByte(); }
        return minus ? -n : n;
    }
    long nl() {
        long n = 0L;    int b = readByte();
        boolean minus = false;
        while(b != -1 && !( (b >= '0' && b <= '9') || b == '-')) { b = readByte(); }
        if(b == '-') { minus = true; b = readByte(); }
        while(b >= '0' && b <= '9') { n = n * 10 + (b - '0'); b = readByte(); }
        return minus ? -n : n;
    }

    double nd() { return Double.parseDouble(ns()); }
    float nf() { return Float.parseFloat(ns()); }
    int[] na(int n) {
        int a[] = new int[n];
        for(int i = 0; i < n; i++) { a[i] = ni(); }
        return a;
    }
    char[] ns(int n) {
        char c[] = new char[n];
        int i, b = skip();
        for(i = 0; i < n; i++) {
            if(isSpaceChar(b)) { break; }
            c[i] = (char)b; b = readByte();
        } return i == n ? c : Arrays.copyOf(c,i);
    }
}
