
import java.lang.*;
import java.util.*;
import java.io.*;
import java.util.Collections; 
import java.util.Comparator;
import java.util.HashMap; 
import java.util.LinkedHashMap; 
import java.util.Map;
import java.util.Map.Entry.*;
import java.util.stream.Collectors;

public class Dominated  {
	static class Number
	{
		int c;
		int st;
		int dist;
		
		public Number(int c,int st,int dist)
		{
			this.c=c;this.st=st;this.dist=dist;
		}
	}
	public static void main(String[] args) {
		
		Scanner sc= new Scanner(System.in);
		int T = sc.nextInt();
		while(T-->0)
		{
        int n = sc.nextInt();
        int[] ar = new int[n];
        Map<Integer,Integer> map = new HashMap<Integer,Integer>();
        for(int i=0;i<n;i++)
        {
        	ar[i]=sc.nextInt();
        	map.put(ar[i], map.getOrDefault(ar[i], 0)+1);
        }
        int max=0,max_num=-1;
        for(Map.Entry<Integer,Integer> entry : map.entrySet())
        {
        	if(entry.getValue() > max) {max = entry.getValue();max_num=entry.getKey();}
        }
        //System.out.println(map.toString()+" "+max_num);
        for(Map.Entry<Integer,Integer> entry : map.entrySet())
        {
        	if(entry.getValue() == max && entry.getKey()!=max_num) {max_num=-1;break;}
        }
        
        if(max_num==-1 || max==1)
        {
        	System.out.println(-1);
        }
        else
        {
        	int j=-1,min=n;
        	for(int i=0;i<n;i++)
        	{
        		if(ar[i] == max_num)
        		{
        			if(j!=-1)
        			{
        				min = Math.min(i-j, min);
        			}
        			j=i;
        		}
        	}
        	System.out.println(min+1);
        }
        
        }
	}
}
