import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;
import java.util.StringTokenizer;

public class C {
    public static void main(String[] args) throws Exception {
        BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
        BufferedWriter bw = new BufferedWriter(new OutputStreamWriter(System.out));
        int q = Integer.parseInt(br.readLine());
        for (int t = 0; t < q; t++) {
            int n = Integer.parseInt(br.readLine());
            int[] a = new int[n + 1];
            int minMas = 2 * n + 1;
            StringTokenizer st = new StringTokenizer(br.readLine());
            for (int i = 1; i <= n; i++) {
                int x = Integer.parseInt(st.nextToken());
                if (a[x] != 0) {
                    minMas = Math.min(minMas, i - a[x] + 1);
                }
                a[x] = i;
            }
            if (minMas == 2 * n + 1) {
                System.out.println(-1);
            } else {
                System.out.println(minMas);
            }
        }
        bw.flush();
    }
}
