import java.util.HashMap;
import java.util.HashSet;
import java.util.Scanner;
 
public class sample
{
    public static void main(String[] args)
    {
        Scanner r=new Scanner(System.in);
 
        int cas1=r.nextInt();
        while(cas1-->0)
        {
            int len1=r.nextInt();
            int[] arr1=new int[len1];
            int min1=Integer.MAX_VALUE;
            HashMap<Integer,Integer> hashMap=new HashMap<>();
            for(int i=0;i<len1;i++)
            {
                int num1=r.nextInt();
                if(!hashMap.containsKey(num1))
                {
                    hashMap.put(num1,i);
                }
                else
                {
                    int prePos=hashMap.get(num1);
                    hashMap.put(num1,i);
                    min1=Math.min(min1,i-prePos+1);
                }
            }
            if(min1==Integer.MAX_VALUE)
            {
                min1=-1;
            }
            System.out.println(min1);
        }
    }
}