import java.util.*;
import java.lang.*;
public class Hello
{
    public static void main(String[] args)
    {
        Scanner sc=new Scanner(System.in);
        int t=sc.nextInt();
        for(int i=0;i<t;i++)
        {
            int n=sc.nextInt();
            int[] arr=new int[n];
            int[] arr1=new int[n+1];
            for(int j=0;j<n;j++)
            {
                arr[j]=sc.nextInt();
                arr1[arr[j]]++;
            }
            int max=Arrays.stream(arr1).max().getAsInt();
            if(max==1)
                System.out.println("-1");
            else
            {
                int[] arr2=new int[n];
                int count=0;
                for(int j=1;j<=n;j++)
                {
                    if(arr1[j]==max)
                        arr2[count++]=j;
                }
                //System.out.println(Arrays.toString(arr2));
                int min=100000000;
                for(int j=0;j<count;j++)
                {
                    int[] arr3 =new int[max];
                    int count1=0;
                    for(int k=0;k<n;k++)
                    {
                        if(arr2[j]==arr[k])
                            arr3[count1++]=k;
                    }
                    //System.out.println(Arrays.toString(arr3));
                    int[] arr4=new int[max-1];
                    for(int k=0;k<count1-1;k++)
                        arr4[k]=arr3[k+1]-arr3[k]+1;
                    //System.out.println(Arrays.toString(arr3)+" "+max);
                    int tmin=Arrays.stream(arr4).min().getAsInt();
                    if(tmin<min)
                        min=tmin;
                }
                System.out.println(min);
            }
        }
    }
}