import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.HashMap;
import java.util.Map;
import java.util.StringTokenizer;

public class Prob_1257_C {

    public static void main(String[] args) throws IOException {
        BufferedReader input = new BufferedReader(new InputStreamReader(System.in));
        int t = Integer.parseInt(input.readLine());
        for(int i = 0; i < t; i++) {
            int n = Integer.parseInt(input.readLine());
            int[] arr = new int[n];
            StringTokenizer data = new StringTokenizer(input.readLine());
            for(int j = 0; j < n; j++) {
                arr[j] = Integer.parseInt(data.nextToken());
            }
            System.out.println(getShortestArr(arr));
        }
    }

    static int getShortestArr(int[] arr) {
        if(arr.length == -1) return -1;
        int res = -1;
        Map<Integer, Integer> positions = new HashMap<>();
        for(int i = 0; i < arr.length; i++) {
            if(positions.containsKey(arr[i])) {
                if(res == -1) res = i-positions.get(arr[i])+1;
                res = Math.min(res, i-positions.get(arr[i])+1);
            }
            positions.put(arr[i], i);
        }
        return res;
    }

}
