#include <bits/stdc++.h>
using namespace std;

#define pb push_back
#define mp make_pair

#define forn(i,n) for(int i = 0;i<int(n);i++)
#define dforn(i,n) for(int i = int(n)-1;i>=0;i--)
#define forsn(i,s,n) for(int i = int(s);i<int(n);i++)
#define fore(i,s,n) for(int i = int(s);i<int(n);i++)
#define dforsn(i,s,n) for(int i = int(n)-1;i>=int(s);i--)

#define DBG(x) do{cout<<#x<<" = "<<x<<endl;}while(false)
#define get(mask,i) (bool(mask&(1<<(i))))
#define set(mask,i) (mask|=(1<<(i)))
#define invert(mask,i) (mask^=(1<<(i)))
#define popcount(mask) (__builtin_popcount(mask))
template<class T> ostream & operator<<(ostream & out, vector<T> & v){
	out<<"[";
	for(auto k : v) out<<k<<" ";
	out<<"]"<<"\n";
	return out;
}
template<class T> ostream & operator<<(ostream & out, set<T>  s){
	out<<"{";
	for(auto k : s) out<<k<<" ";
	out<<"}"<<"\n";
	return out;
}

template<class T, class U> ostream & operator<<(ostream & out, pair<T,U>  p){
	out<<"[ "<<p.first<<" , "<<p.second<<" ] ";
	return out;
}
template<class T, class U> istream & operator>>(istream & in, pair<T,U> & p){
	in>>p.first>>p.second;
	return in;
}


typedef long long intl;
int main()
{
    cin.tie(0);
    cin.sync_with_stdio(0);
    int t;cin>>t;
    while(t--){
        int n;cin>>n;

        vector<int> in(n);
        vector<int> fin(n+2,0), app(n+2);
        int k = 0;
        forn(i,n){
            cin>>in[i];
            if(i && in[i]==in[i-1])
                fin[in[i]]++, fin[in[i-1]]++,k++;
            app[in[i]]++;
        }
        fin[in[0]]++, fin[in.back()]++;
        int m = *max_element(app.begin(),app.end());
        if(m*2>(n+1)) cout<<-1<<"\n";
        else{
            cout<<k+max(0,*max_element(fin.begin(),fin.end())-(k+2))<<"\n";

        }
        /*map<int,int> sob;
        vector<int> app(n+1,0), malo(n+1,false);
        int cost = 0;

        forn(i,n){
            cin>>in[i];
            if(i&&in[i]==in[i-1]) sob[in[i]]++, cost++, malo[i]++;

            app[in[i]]++;
        }
        int sup = 0;
        forsn(i,1,n-1){
            if(in[i]!=in[i-1] && malo[i-1] && malo[i+1]){
                sup+=2;
                cost-=2;
                sob[in[i-1]]--;
                sob[in[i+1]]--;
            }
        }
        int mapp = *max_element(app.begin(),app.end());
        if(sob.size()==0){
            cout<<0<<"\n";
            continue;
        }
        if(mapp*2>(n+1)){
            cout<<-1<<"\n";
            continue;
        }
        int maxc = 0, ex = 0;
        for(auto k : sob){
            if(k.second>maxc) maxc = k.second, ex = k.first;
        }
        if(maxc*2>cost){
            int inic = cost;
            //DBG(inic);
            int d = cost/2;
            cost += (maxc-d)+1;
            int r = 0;
            if(ex!=in.back())r++;
            if(ex!=in[0])r++;
            cost -= min(r,inic);
        }
        else if(maxc*2==cost){
            cost++;
            if(ex!=in[0] || ex!=in.back()) cost--;
        }
        cout<<cost+sup<<"\n";*/
    }
    return 0;
}
