import java.util.*;
import java.io.*;
public class cdfed76a
{
	public static void main(String args[])throws Exception
	{
		BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
		int n=Integer.parseInt(br.readLine());
		for(int i=0;i<n;i++)
		{
			int t=Integer.parseInt(br.readLine());
			String s=br.readLine();
			String ss[]=s.split(" ");
			int arr[]=new int[t];
			for(int j=0;j<t;j++)
			arr[j]=Integer.parseInt(ss[j]);
			int ans[][]=new int[t+2][3];
			int min=t+5,f=0;
			for(int j=0;j<t;j++)
			{
				ans[arr[j]][0]++;
				if(ans[arr[j]][0]==2)
				{
					f=1;
					ans[arr[j]][2]=j-ans[arr[j]][1]+1;
					if(ans[arr[j]][2]<=min)
						min=ans[arr[j]][2];
					ans[arr[j]][0]=1;
					ans[arr[j]][1]=j;
				}
			}
			if(f==1)
			System.out.println(min);
			else
			System.out.println("-1");
		}
	}
}
