import java.util.*;
import java.io.*;

public class q3 {

    public static void main(String args[]) throws Exception {
        BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
        String str[] = br.readLine().split(" ");

        int test = Integer.parseInt(str[0]);

        while(test-- > 0) {
            str = br.readLine().split(" ");

            int n = Integer.parseInt(str[0]);

            str = br.readLine().split(" ");
            int arr[] = new int[n];
            for(int i=0; i<n; i++) {
                arr[i] = Integer.parseInt(str[i]);
            }

            HashMap<Integer, Integer> lastOccurance = new HashMap<Integer, Integer>();
            HashMap<Integer, Integer> diff = new HashMap<Integer, Integer>();

            for(int i=0; i<n; i++) {
                if(!lastOccurance.containsKey(arr[i])) {
                    lastOccurance.put(arr[i], i);
                }
                else {
                    if(diff.containsKey(arr[i])) {
                        diff.put(arr[i], Math.min(diff.get(arr[i]), i - lastOccurance.get(arr[i]))+1);
                    }
                    else {
                        diff.put(arr[i], i - lastOccurance.get(arr[i])+1);
                    }
                    lastOccurance.put(arr[i], i);
                }
            }

            int min = Integer.MAX_VALUE;
            if(diff.isEmpty()) {
                System.out.println("-1");
            }
            else {
                for(Integer key : diff.keySet()) {
                    if(min > diff.get(key)) {
                        min = diff.get(key);
                    }
                }
                System.out.println(min);
            }
        }
    }
}