import java.util.*;
public class asd
{
         public static void main(String args[])
         {
             Scanner s=new Scanner(System.in);
             int t=s.nextInt();
             while(t-->0)
             {
                 int n=s.nextInt();
                 int arr[]=new int[n];
                  HashMap<Integer, Integer> freqMap = new HashMap<Integer, Integer>(); 
                  ArrayList<Integer> list=new ArrayList<>();
                 for(int i=0;i<n;i++)
                 {
                     arr[i]=s.nextInt();
                     if (freqMap.containsKey(arr[i]))
                     { 
                         list.add(i-freqMap.get(arr[i])+1);
                         freqMap.put(arr[i], i); 
                
                     } 
                      else
                     { 
                        freqMap.put(arr[i], i); 
                      } 
                     
                 }
                 if(!list.isEmpty())
                 {
                 Collections.sort(list);
                 System.out.println(list.get(0));
                 }
                 else
                     System.out.println("-1");
             }
            
         }
         
}
         
         
