import java.io.*;
import java.util.*;
import java.text.*;
import java.math.*;
import java.util.regex.*;

public class Solution {

    public static void main(String[] args) throws IOException{
        BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
        
        int t=Integer.parseInt(br.readLine());
        while(t-->0){
            HashMap<Integer,Integer> h =new HashMap<Integer,Integer>();
            int n=Integer.parseInt(br.readLine());
            String[] str=br.readLine().split(" ");
            
        
            int[] arr=new int[n];
        for(int i=0;i<n;i++){
            arr[i]=Integer.parseInt(str[i]);
        }
        int min=Integer.MAX_VALUE;
        for(int j=0;j<n;j++){
            
            if(h.get(arr[j])!=null){
                min=Math.min(min,j-h.get(arr[j])+1);
            }
            
                h.put(arr[j],j);
            
        }
            if(min==Integer.MAX_VALUE) System.out.println(-1);
            else System.out.println(min);
    }  
    }
}