
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,Number> map = new HashMap<Integer,Number>();
        for(int i=0;i<n;i++)
        {
        	ar[i]=sc.nextInt();
        	if(map.containsKey(ar[i]))
        	{
        		map.get(ar[i]).c+=1;
        		if(map.get(ar[i]).dist == -1)
        			map.get(ar[i]).dist = i - map.get(ar[i]).st+1;
        		else
        			map.get(ar[i]).dist = Math.min(map.get(ar[i]).dist, i - map.get(ar[i]).st+1);
        		map.get(ar[i]).st = i;
        	}
        	else
        	{
        		map.put(ar[i], new Number(1,i,-1));
        	}
        }
        Comparator<Number> byCount = (Number o1, Number o2)-> o2.c - o1.c;
        
        LinkedHashMap<Integer, Number> sortedMap = map.entrySet().stream()
                .sorted(Map.Entry.<Integer, Number>comparingByValue(byCount))
                .collect(Collectors.toMap(Map.Entry::getKey,Map.Entry::getValue,(e1,e2)->e1,LinkedHashMap::new));
        
        List<Integer> numbers = new ArrayList(sortedMap.keySet());
        
      /**  for(Integer i : sortedMap.keySet())
        {
        	System.out.println("Keys = "+i+" "+" Count = "+sortedMap.get(i).c+" Start = "+sortedMap.get(i).st+" Dist = "+sortedMap.get(i).dist);
        }
        **/
        int l = numbers.size() , max = sortedMap.get(numbers.get(0)).c,min=n;
        
        if(max < 2)
        {
        	System.out.println(-1);
        }
        else
        {
        for(int i=0;i<l;i++)
        {
        	if(sortedMap.get(numbers.get(i)).c < max)break;
        	min  = Math.min(min, sortedMap.get(numbers.get(i)).dist);
        }
        System.out.println(min);
        }
        
		}
	}
}
