import java.util.HashMap;
import java.util.Scanner;

public class Question_3 {
    public static void main(String[] args) {
        Scanner in = new Scanner(System.in);

        int t = in.nextInt();

        for(int i=0; i<t; i++){
            int n = in.nextInt();
            int[] arr = new int[n];
            for(int j=0; j<n; j++){
                arr[j] = in.nextInt();
            }
            System.out.println(getSolution(arr));
        }


    }
    private static int getSolution(int[] arr){
        int cur1 = 0, cur2 = 0, min = Integer.MAX_VALUE;

        HashMap<Integer, Integer> map = new HashMap<>();

        if(arr.length < 2){
            return -1;
        }

        for(; cur1<arr.length; cur1++){

            if(!map.containsKey(arr[cur1])){
                map.put(arr[cur1], 1);
            }else{
                map.put(arr[cur1], map.get(arr[cur1])+1);
            }

            if(map.get(arr[cur1]) == 2){
                //System.out.println(cur1 + " " + cur2);
                min = Math.min(min, cur1-cur2+1);
                if(min == 2){
                    return min;
                }

                while(cur1-cur2+1 >= min){
                    map.put(arr[cur2], map.get(arr[cur2])-1);
                    cur2++;
                }

            }

        }

        return min == Integer.MAX_VALUE ? -1 : min;

    }

}
