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

/**
 * Solution by: Bashar Abu Joub
 * Problem: DominatedSubarray
 */

public class DominatedSubarray {


    private void solve(FastInput in, PrintWriter out) {
        int t = in.nextInt();
        while((t--) > 0){
            int size = in.nextInt();


            int arr[] = new int[size];
            for (int i = 0; i < size; i++) {
                arr[i] = in.nextInt();
            }

            if(size == 1){
                out.println("-1");
                continue;
            }

            Map<Integer, ArrayList<Integer>> occurence = new HashMap<>();

            for (int i = 0; i < size; i++) {
                if(occurence.get(arr[i]) == null){
                    ArrayList<Integer> subOccurence = new ArrayList<>();
                    subOccurence.add(i);
                    occurence.put(arr[i], subOccurence);
                }else{
                    occurence.get(arr[i]).add(i);
                }
            }

            int min = Integer.MAX_VALUE;
            for(int i : occurence.keySet()){
                if(occurence.get(i).size() <= 1) continue;
                ArrayList<Integer> sub = occurence.get(i);
                for (int j = 0; j < sub.size() - 1; j++) {
                    if((sub.get(j+1) - sub.get(j)) + 1 < min)
                        min = (sub.get(j+1) - sub.get(j)) + 1;
                }
            }
            out.println(min == Integer.MAX_VALUE ? "-1" : min);
        }

    }


    public static void main(String[] args) {
        PrintWriter writer = new FastOutput().getWriter();
        new DominatedSubarray().solve(new FastInput(), writer);
        writer.close();
    }


    static class FastInput {

        BufferedReader reader;
        StringTokenizer tokenizer;

        public FastInput() {
            reader = new BufferedReader(new InputStreamReader(System.in));
        }

        public String next() {
            while (tokenizer == null || !tokenizer.hasMoreElements()) {
                try {
                    tokenizer = new StringTokenizer(reader.readLine());
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
            return tokenizer.nextToken();
        }

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


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

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

        public String nextLine() {
            String str = "";
            try {
                str = reader.readLine();
            } catch (IOException e) {
                e.printStackTrace();
            }
            return str;
        }

    }

    static class FastOutput {

        BufferedOutputStream bufferedOutputStream;
        PrintWriter writer;

        public FastOutput() {
            writer = new PrintWriter(new BufferedOutputStream(System.out));
        }

        public PrintWriter getWriter() {
            return this.writer;
        }


    }

}

