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

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

        int t = in.nextInt();

        while (t-- > 0){
            int n = in.nextInt();

            int[] arr = new int[n];

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

            System.out.println(dominatedSubarray(arr, arr.length));
        }
    }
    // A dominated subarray is an array which have one majority frequency element.
    private static int dominatedSubarray(int[] arr , int length) {
        if (length < 2){
            return -1;
        }

        // To map frequency of every element of array
        Map<Integer, Integer> map = new HashMap<>();

        // Track of shortest length of dominated subarray
        int shortest = Integer.MAX_VALUE;

        //First pointer
        int i=0;

        //Second pointer
        for (int j = 0; j < length; j++) {
            //GROW : Till you get the valid dominated subarray.
            map.put(arr[j], map.getOrDefault(arr[j], 0)+1);

            //SHRINK : When you again get an invalid window.
            while (map.get(arr[j]) > 1){
                shortest = Math.min(shortest, j-i+1);
                map.put(arr[i], map.getOrDefault(arr[i], 0)-1);

                if (map.get(arr[i]) == 0){
                    map.remove(arr[i]);
                }

                ++i;//incrementing i
            }
        }
        return (shortest == Integer.MAX_VALUE)? -1 : shortest;
    }
}