/* package whatever; // don't place package name! */

import java.util.*;
import java.lang.*;
import java.io.*;

/* Name of the class has to be "Main" only if the class is public. */
public class Ideone
{
	public static void main (String[] args) throws java.lang.Exception
	{
		// your code goes here
		Scanner sc = new Scanner(System.in);
		int test = sc.nextInt();
		while(test-- > 0)
		{
			int n = sc.nextInt();
			int[] a = new int[n];
			HashMap<Integer,Integer> mp = new LinkedHashMap<>();
			int occure = 1,most=0;
			for(int i=0;i<n;i++)
			{
				a[i] = sc.nextInt();
				if(!mp.containsKey(a[i]))
				{
					mp.put(a[i],1);
				}
				else
				{
					mp.put(a[i],mp.get(a[i])+1);
					if(occure < mp.get(a[i]))
					{
						occure = mp.get(a[i]);
						most = a[i];
					}
					
				}
			}
			int cnt = 0,len = 0;
			for(int i : mp.keySet())
			{
				if(mp.get(i) == occure)
					cnt++;
			}
			if(a.length < 2)
			{
			    System.out.println("-1");
			}
			else
			if(cnt == 1)
			{
				int start = -1,end = 0,min = Integer.MAX_VALUE;
				for(int i=0;i<a.length;i++)
				{
					if(start == -1 && mp.get(a[i]) == occure)
					{
						start = i;
					}
					else if(mp.get(a[i]) == occure)
					{
						end = i;

						if(min > (end - start+1))
							min = end -start+1;
						start = end;
					}
	
				}
				if(min == Integer.MAX_VALUE)
				    System.out.println("-1");
				else
				    System.out.println(min);
			}
			else
			{
				System.out.println("-1");
			}
			
		}
		
	}
}