import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.util.HashMap;
import java.util.HashSet;
import java.util.LinkedList;
import java.util.Map;
import java.util.Queue;
import java.util.Set;
import java.util.StringTokenizer;

public class C {
    void solve() throws IOException {
        int t = nextInt();
        for (int i = 0; i < t; ++i) {
            int n = nextInt();
            int[] arr = new int[n];
            for (int j = 0; j < n; ++j) {
                arr[j] = nextInt();
            }
            System.out.println(doSolve(arr));
        }
    }

    private static int doSolve(int[] arr) {
        int shortestLength = Integer.MAX_VALUE;
        Map<Integer, Integer> elLeftInd = new HashMap<>();
        Queue<Integer> q = new LinkedList();
        for (int i = 0; i < arr.length; ++i) {
            int el = arr[i];
            if (elLeftInd.containsKey(el)) {
                int leftInd = elLeftInd.get(el);
                shortestLength = Math.min(i - elLeftInd.get(el) + 1, shortestLength);
                while(!q.isEmpty() && q.peek() <= leftInd) {
                    elLeftInd.remove(q.poll());
                }
            }
            elLeftInd.put(el, i);
            q.offer(i);
        }
        return shortestLength == Integer.MAX_VALUE ? -1 : shortestLength;
    }

    String nextToken() throws IOException {
        while (tokenizer == null || !tokenizer.hasMoreTokens()) {
            tokenizer = new StringTokenizer(reader.readLine());
        }
        return tokenizer.nextToken();
    }

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

    StringTokenizer tokenizer;
    PrintWriter writer;
    BufferedReader reader;

    public void run() {
        try {
            writer = new PrintWriter(System.out);
            reader = new BufferedReader(new InputStreamReader(System.in));
            solve();
            reader.close();
            writer.close();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    public static void main(String[] args) {
        new C().run();
    }
}
