#include<bits/stdc++.h>
using namespace std;
#define int long long
typedef long long ll;
long long t,n,m,k,ccnt,pos=0,sum=0,minn2=INT_MAX,sum2,cnt=0,cnt1=0,cnt2=0,cnt3=0,cnt4=0,cnt5=0,cnt6=0,cnt7=0,a[2000005],x1,y7,x2,y2,x3,y3,aaa,bbb;
long long vis[1000005],b[1000005],c[1000005],c3[2005][2005],c4[1000005],kk[1000005],x,y,z,d=0,l2,r2;
long long pos1=0,pos2=0,tem,tem2,l,r,ans=0,ans2,mod=998244353,tw[100005],th[100005],dd,ee,out[1000005],te[100005],dp[1000005],dp2[1000005],dp3[1000005];
long long minn=LONG_LONG_MAX,maxx=-LONG_LONG_MAX;
string s1,s2,s;
char cc[2005][2005],ch,cr[100005],ch2;
vector<int> v[100005];
//vector<int> v2[100005];
long long fpow(long long aa,long long bb)
{
    long long kk1=aa;
    long long kk2=1;
    while(bb>1)
    {
        if(bb%2)
        {
            kk2*=kk1;
            kk2%=mod;
        }
        kk1*=kk1;
        kk1%=mod;
        bb=bb/2;
    }
    return (kk1*kk2)%mod;
}
signed main()
{
    cin.tie(0);
    cout.tie(0);
    ios_base::sync_with_stdio(0);
    cin>>t;
    while(t--)
    {
        cin>>n;
        maxx=0;
        for(int i=0;i<n;i++)
        {
            cin>>a[i];
            c[a[i]]++;
            maxx=max(maxx,c[a[i]]);
        }
        if(maxx>(n+1)/2)
        {
            cout<<-1<<'\n';
        }
        else
        {
            cnt=0;
            maxx=0;
            for(int i=0;i<n;i++)
            {
                b[a[i]]++;
                maxx=max(maxx,b[a[i]]);
                if(a[i]!=a[i+1]&&i+1<n)
                {
                    while(a[i]!=a[i+1]&&i+1<n)
                    {
                        i++;
                    }
                }
                cnt++;
                b[a[i]]++;
                maxx=max(maxx,b[a[i]]);
            }
            cout<<cnt-1+max(0LL,maxx-cnt-1)<<'\n';
            for(int i=0;i<n;i++)
            {
                b[a[i]]=0;
            }
        }
        for(int i=0;i<n;i++)
        {
            c[a[i]]=0;
        }



    }


}
