1
3
5 3 4
 6
5
5 3 4 4 5
3
1 1 1
5
4 4 3 4 4
5
5 5 4 3 2
3
1 1 2
5
5 4 3 5 5
 1
3
10 1 5

1
 1
-1
2
2
3
1
 1

import java.util.ArrayList;
import java.util.Scanner;

public class FirstPage {

    public static void main(String[] args)
    {
        Scanner sc = new Scanner(System.in);
        int t = sc.nextInt();
        while (t>0){
            int n=sc.nextInt();
            ArrayList<Long> al =new ArrayList<>();
            for(int i=0;i<n;i++){
                al.add(i,sc.nextLong());
            }
            int MaxI=0;
            long Max=al.get(0);
            for(int i=1;i<al.size();i++){
                if(al.get(i)>Max){
                    Max=al.get(i);
                    MaxI=i;
                }
            }
            int Z=MaxI;
            long l,c,r;
            boolean b=true;
            while (b){
                c=al.get(MaxI);
                if(MaxI-1>=0){
                    l=al.get(MaxI-1);
                    if(l<c){
                        c++;
                        al.set(MaxI,c);
                        al.remove(MaxI-1);
                        MaxI--;
                        continue;
                    }
                }
                if(MaxI+1<al.size()){
                    r=al.get(MaxI+1);
                    if(r<c){
                        c++;
                        al.set(MaxI,c);
                        al.remove(MaxI+1);
                        continue;
                    }
                }
                if(MaxI+1<al.size()){
                    if(al.get(MaxI+1)==al.get(MaxI)){
                        MaxI++;
                        Z++;
                        continue;
                    }
                }
                b=false;
            }
            if(al.size()==1){
                System.out.println(Z+1);
            }
            else {
                System.out.println("-1");
            }
            t--;
        }
    }
}

