    import java.util.*;
    import java.lang.*;
    import java.io.*;
   public class Solution {
       public static void main(String args[]){
           
       Scanner sc=new Scanner(System.in);
       int t=sc.nextInt();
       while(t-->0){
           int n=sc.nextInt();
           int arr[]=new int[n];
           int i=0;
           for(i=0;i<n;i++){
               arr[i]=sc.nextInt();
           }
           HashMap<Integer,Integer> a=new HashMap<>();
           int min=1000000,d=0;
           for(i=0;i<n;i++){
               if(a.containsKey(arr[i])){
                   d=i-a.get(arr[i]);
                   if(min>d)
                   min=d;
                   a.put(arr[i],i);
               }
               else{
                   a.put(arr[i],i);
               }
           }
           if(min==1000000)
           System.out.println("-1");
           else
           System.out.println(min+1);
       }
       }}