import java.util.ArrayList;
import java.util.Collections;
import java.util.HashMap;
import java.util.Map;
import java.util.Map.Entry;
import java.util.Scanner;
import java.util.Set;
public class DominatedSubarray {

	public static void main(String[] args)
	{
		Scanner obj=new Scanner(System.in);
		int t=obj.nextInt();
		while(t-->0)
		{
			int n=obj.nextInt();
			HashMap<Integer,Integer> map=new HashMap<>();
			ArrayList<Integer> arr=new ArrayList<Integer>();
			ArrayList<Integer> idx=new ArrayList<Integer>();
			for(int i=0;i<n;i++)
			{
				int a=obj.nextInt();
				arr.add(a);
				
				if(!map.containsKey(a))
					map.put(a,1);
				else
				{
					int value=map.get(a);
					value++;
					map.put(a,value);	
				}
				
				
			}
			int max=0;
			int maxch=0;
			Set<Entry<Integer,Integer>> entries=map.entrySet();
			for(Entry<Integer, Integer>  entry: entries)
			{
				if(entry.getValue()>max)
				{
					max=entry.getValue();
					maxch=entry.getKey();
				}
			}
			for(int i=0;i<arr.size();i++)
			{
				if(arr.get(i)==maxch)
				{
					idx.add(i);
				}
			}
			Collections.sort(idx);
			int diff=Integer.MAX_VALUE;//how to define a number as infinite
			for(int i=0;i<idx.size()-1;i++)
			{
				if(idx.get(i+1)-idx.get(i)<diff)
				{
					diff=idx.get(i+1)-idx.get(i);
				}
			}
			//System.out.println(idx);
			if(idx.size()!=1)
			System.out.println(diff+1);
			else
				System.out.println(-1);
			
			
		}


	}

}
