import java.util.*;
public class prac{
    public static void main(String args[]){
        Scanner sc=new Scanner(System.in);
        int t;
        t=0;
        t=sc.nextInt();
        while(t>0){
            t--;
            int n;
            n=sc.nextInt();
            int arr[]=new int[n];
            int i,max;
            max=0;
            HashMap<Integer,Integer> hmap=new HashMap<>();
            for(i=0;i<n;i++){
                arr[i]=sc.nextInt();
                if(hmap.containsKey(arr[i])){
                    hmap.put(arr[i],hmap.get(arr[i])+1);//issue +1
                }
                else{
                    hmap.put(arr[i],1);
                }
                if(hmap.containsKey(arr[i]) && hmap.get(arr[i])>max){
                    max=hmap.get(arr[i]);
                }
            }
            int cnt=0;
            int val=0;
            for(int u:hmap.keySet()){//keySet()
                if(hmap.get(u)==max){
                    cnt++;
                    val=u;
                }
            }
            int pos,prevpos;
            pos=0;
            prevpos=0;
            if(cnt>=2 || max<=1){
                System.out.println("-1");
                continue;//jump statement
            }
            else{
                cnt=0;
               for(i=0;i<n && cnt<=max;i++){
                   if(arr[i]==val){
                      cnt++;
                      if(cnt>=1){
                          prevpos=pos;
                      }
                      pos=i;
                   }
               } 
            }
            pos++;
            //prevpos++;
            int dif=pos-prevpos;
            System.out.println(dif);
        }
    }
}