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

public class Solution {
    public static void main(String[] args) throws IOException {
        FastReader in = new FastReader(System.in);
        StringBuilder sb = new StringBuilder();
        int i,j;
        int t=in.nextInt();
        while(t-->0) {
            int n = in.nextInt();
            int arr[] = new int[n+1];
            for (i = 1; i <= n; i++)
                arr[i] = in.nextInt();
            HashSet<Integer> set=new HashSet<>();
            int pos[]=new int[n+1];
            int dist[]=new int[n+1];
            int min=Integer.MAX_VALUE;
            for(i=1;i<=n;i++) {
                set.add(arr[i]);
                dist[i]=set.size();
                if(pos[arr[i]]==0)
                {
                    pos[arr[i]]=i;
                }
                else
                {
                    //System.out.println(dist[i]+" "+dist[pos[arr[i]]]+" "+i+" "+pos[arr[i]]);
                    min=Math.min(min,i-pos[arr[i]]+1);
                    pos[arr[i]]=i;
                }
                //System.out.println("pos->"+Arrays.toString(pos));
                //System.out.println("dist->"+Arrays.toString(dist));
            }

            if(min==Integer.MAX_VALUE)
                min=-1;
            sb.append(min).append("\n");
        }

        System.out.println(sb);

    }
}
class FastReader {

    byte[] buf = new byte[2048];
    int index, total;
    InputStream in;

    FastReader(InputStream is) {
        in = is;
    }

    int scan() throws IOException {
        if (index >= total) {
            index = 0;
            total = in.read(buf);
            if (total <= 0) {
                return -1;
            }
        }
        return buf[index++];
    }

    String next() throws IOException {
        int c;
        for (c = scan(); c <= 32; c = scan()) ;
        StringBuilder sb = new StringBuilder();
        for (; c > 32; c = scan()) {
            sb.append((char) c);
        }
        return sb.toString();
    }

    String nextLine() throws IOException {
        int c;
        for (c = scan(); c <= 32; c = scan()) ;
        StringBuilder sb = new StringBuilder();
        for (; c != 10 && c != 13; c = scan()) {
            sb.append((char) c);
        }
        return sb.toString();
    }

    char nextChar() throws IOException {
        int c;
        for (c = scan(); c <= 32; c = scan()) ;
        return (char) c;
    }

    int nextInt() throws IOException {
        int c, val = 0;
        for (c = scan(); c <= 32; c = scan()) ;
        boolean neg = c == '-';
        if (c == '-' || c == '+') {
            c = scan();
        }
        for (; c >= '0' && c <= '9'; c = scan()) {
            val = (val << 3) + (val << 1) + (c & 15);
        }
        return neg ? -val : val;
    }

    long nextLong() throws IOException {
        int c;
        long val = 0;
        for (c = scan(); c <= 32; c = scan()) ;
        boolean neg = c == '-';
        if (c == '-' || c == '+') {
            c = scan();
        }
        for (; c >= '0' && c <= '9'; c = scan()) {
            val = (val << 3) + (val << 1) + (c & 15);
        }
        return neg ? -val : val;
    }
}