import java.util.*;
import java.util.Map.*;

public class A
{
    public static void main(String[] agrgs)
    {
        
        Scanner in=new Scanner(System.in);
        int x=in.nextInt();
        while(x--!=0)
        {
            int y=in.nextInt();
            int[] arr=new int[y];
            for(int i=0;i<y;i++)
            {
                arr[i]=in.nextInt();
            }
            fun(arr,y);
        }
    }

    public static void fun(int[] arr, int y)
    {
        if(arr.length==1)
        System.out.println(-1);
        else
        {HashMap<Integer,Integer> hm=new HashMap<Integer,Integer>();
        for(int i=0;i<y;i++)
        {
          if(!hm.containsKey(arr[i]))
          hm.put(arr[i],1);
          
          else
          {
              int y1=hm.get(arr[i]);
              hm.replace(arr[i],y1+1 );
          }
        }
        
        int maxValue=Collections.max(hm.values());
        ArrayList<Integer> arr1=new ArrayList<Integer>();    
        for(Entry<Integer,Integer> entry: hm.entrySet())
        if (entry.getValue()==maxValue) {
               arr1.add(entry.getKey());
               
            }
            
            if(arr1.size()>1)
            System.out.println("-1");
            
            else
            {
                ArrayList<Integer> arr2=new ArrayList<Integer>();
              int z=arr1.get(0);
              for(int i=0;i<y;i++)
              {
                  if(arr[i]==z)
                  {
                    arr2.add(i);  
                  }
              }

               
              int min=(arr2.get(1)-arr2.get(0)); 
              for(int i=2;i<arr2.size();i++)
              {
                int z1=(arr2.get(i)-arr2.get(i-1));
                if(z1<min)
                min=z1;
              }
              System.out.println(min+1);
            }
            
        }  
            
        
        
        
        
        
        
        
        
        
        
        
        
        
        
        
        
    }
}







