
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.HashSet;
import java.util.Set;

public class DominatedSubarray{
    public static void main(String[] args) throws IOException {
        BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
        int t = Integer.parseInt(br.readLine());
        for (int test = 0; test < t; test++) {
            int n = Integer.parseInt(br.readLine());
            String[] in = br.readLine().split(" ");
            int arr[]=new int[n];
            for(int i=0;i<n;i++){
                arr[i] = Integer.parseInt(in[i]);
            }
            int max = Integer.MAX_VALUE;
            Set<Integer> k = new HashSet<>();
            int l=0,h=0;
            while(l<n && h<n){
                if(!k.contains(arr[h])){
                    k.add(arr[h]);
                    h++;
                }else{
                    max = Integer.min(max,h-l+1);
                    if(arr[l]!=arr[h]){
                        while (l < n && arr[l] != arr[h]) {
                            k.remove(arr[l]);
                            l++;
                            max = Integer.min(max, h - l + 1);
                        }
                    }
                    l++;
                    h++;
                }
            }
            if(max==Integer.MAX_VALUE){
                System.out.println(-1);
            }else{
                System.out.println(max);
            }
        }
    }
}
