#include <iostream>
#include <list>
#include <cstdio>
#include <cstdlib>
#include <algorithm>
#include <cmath>
#include <vector>
#include <set>
#include <stack>
#include <map>
#include <unordered_set>
#include <unordered_map>
#include <queue>
#include <ctime>
#include <cassert>
#include <complex>
#include <string>
#include <cstring>
#include <chrono>
#include <random>
#include <bitset>
using namespace std;

using ll = long long;

using ull = unsigned long long;

using pi=pair<int,int>;

using pl=pair<ll,ll>;

#define pb push_back


#define     f               first
#define     s               second
#define     rep(i,a,b)      for(ll i=a;i<b;i++)
#define     repd(i,b,a)     for(ll i=b;i>a;i--)
#define     repi(i,b,a,c)   for(ll i=b;i<a;i+=c)
#define     cas             ll cas;cin>>cas ; for(ll i=0;i<cas;i++)

const ll N=3e5 + 10;
const ll INF=1e10;
const ll M=1e3 +1;
const ll L=31;
const ll mod=1e9 +7;

ll solve(){
    ll n;
    cin>>n;
    ll arr[n];
    ll cnt[n+1];
    ll end[n+1];
    memset(cnt,0,sizeof cnt);
    memset(end,0,sizeof end);
    ll mx=0,c=0;
    for(ll i=0;i<n;i++){
        cin>>arr[i];
        cnt[arr[i]]++;
        mx=max(mx,cnt[arr[i]]);
        if(i-1>=0 && arr[i]==arr[i-1]){
            end[arr[i]]++;
            end[arr[i-1]]++;
            c++;
        }
    }
    if(mx>((n+1)/2)){
        cout<<-1<<'\n';
        return 0;
    }
    end[arr[0]]++;
    end[arr[n-1]]++;
    mx=*max_element(end,end+n+1);
    cout<<c+max(0ll,mx-(c+2))<<'\n';
    return 0;
}
signed main(){
    
    cas{
        solve();
    }    
    return 0;
}