import java.util.Scanner;
import java.util.Map;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Collections;

public final class Main {
    public static void main(String[] args) {
        Scanner inputUser = new Scanner(System.in);
        Integer numberOftests = inputUser.nextInt();

        ArrayList<Integer> m = new ArrayList<>();


        for (int l =0; l < numberOftests; ++l) {
            Integer n = inputUser.nextInt();

            Map<Integer, Integer> map = new HashMap<Integer, Integer>(); 
            ArrayList<Integer> array = new ArrayList<>();

            Integer newKey = 0;
            for (int i =0; i < n; ++i) {
                newKey = inputUser.nextInt();
                    array.add(newKey);
                
                if ( !map.containsKey(newKey) ) {
                    map.put(newKey, 1);
                } 
                else {
                    map.put(newKey, map.get(newKey)+1);
                }  
            }
            
            Integer keySelected = Collections.max(map.entrySet(), Map.Entry.comparingByValue()).getKey();      
            Integer maxValue = map.get(keySelected);     

            int quant = 1;
            for (Map.Entry<Integer,Integer> entry : map.entrySet() ) {
                if (entry.getKey() !=  keySelected && entry.getValue() == maxValue) {
                    ++quant;
                }

            }


            if (maxValue <= 1 || quant > 1) {
                m.add(-1);
            }
            else {
                Integer firstOcurrence =-1;
                Integer secondOcurrence = -1;

                for (int i = 0; i < n; ++i) {
                    if ( array.get(i) == keySelected) {
                        if( firstOcurrence == -1)
                            firstOcurrence = i;
                        else if (secondOcurrence == -1) {
                            secondOcurrence = i;
                        }
                        else {

                            if ( (secondOcurrence-firstOcurrence+1) > (i-secondOcurrence+1) ) {
                                firstOcurrence = secondOcurrence;
                                secondOcurrence =  i;
                            }
                        }
                    }
                }
                m.add(secondOcurrence-firstOcurrence+1);
            }
        }
        for ( Integer k: m ) {
            System.out.println(k);
        }
    }
}
 	 	 	 	 	 		  			  	   		  		