import java.io.PrintWriter;
import java.util.Arrays;
import java.util.Scanner;

public class _1257C {
    public static void main(String args[]){
        Scanner in = new Scanner(System.in);
        PrintWriter out = new PrintWriter(System.out);
        int T = in.nextInt();
        for(int i = 0; i < T; i++){
            int n = in.nextInt();
            int dist[] = new int[n + 1];
            Arrays.fill(dist,-1);
            int result = Integer.MAX_VALUE;
            for(int j = 0; j < n; j++){
                int temp = in.nextInt();
                if(dist[temp] == -1)
                    dist[temp] = j;
                else{
                    result = Math.min(Math.abs(dist[temp] - j) + 1, result);
                    dist[temp] = j;
                }
            }
            out.println(result == Integer.MAX_VALUE ? -1 : result);
        }

        out.close();
    }
}
