#include<bits/stdc++.h> 
#define fp(x,a,b) for(ll x=a; x<b; x++)
#define fn(x,a,b) for(int x=a; x>b; x--)
#define f(x, m) for(auto x : m)
#define cpu() ios::sync_with_stdio(false); cin.tie(nullptr)
#define pb push_back
#define pii pair<int,int>
#define pll pair<ll, ll>
#define vi vector<int>
#define vl vector<ll>
#define vii vector<pair<int ,int>>
#define vll vector<pair<ll ,ll>>
#define all(v) v.begin(),v.end()
#define sor(a) sort( a.begin(), a.end() )
#define ros(a) sort( a.rbegin(), a.rend())
#define prec(n) fixed<<setprecision(n)
#define ff first
#define ss second
// #define tt third
#define print(x) for(auto it:x) cout<<it<<" ";
#define debug(x) cerr << #x << " is " << x << endl;
#define w(x) while(x--)
#define r(x) scanf("%d", &x)
#define rl(x) scanf("%ld", &x) 
#define rll(x) scanf("%lld", &x)

typedef long long ll;
using namespace std;
template <typename TYPE> void readint(TYPE &x){
x=0; int f=1; char c;
for(c=getchar();!isdigit(c);c=getchar())if(c=='-')f=-1;
for(;isdigit(c);c=getchar())x=x*10+c-'0';
x*=f;
}
#define dbg(args...){ string _s = #args; replace(_s.begin(), _s.end(), ',', ' '); stringstream _ss(_s); istream_iterator<string> _it(_ss); err(_it, args); }
void err(istream_iterator<string> it) {}
template<typename T, typename... Args>
void err(istream_iterator<string> it, T a, Args... args) {
cerr << *it << " = " << a << endl;
err(++it, args...);
}
template<typename... T>
void rd(T& ... args){
  ((cin >> args), ...);
}
template<typename... T>
void ps(T ... args){
  ((cout << args << ' '), ...);
  cout<<'\n';
}
const ll MOD = 1e9 + 7, MOD1 = 998244353LL, MAX = 1e5 + 5;
const char nl = '\n';
const int INF = 2e9;
int n;
int a[MAX], b[MAX], c[MAX];
//queue front back push pop
//stack top push pop
// __builtin_popcount() : Cuenta todos los bits 1 de un número
// __builtin_parity() : Revisa la paridad de un número
// __builtin_clz() : Cuenta todos los bits 0 a la izquierda del primer bit 1 de un número.
// __builtin_ctz() : Cuenta todos los bits 0 a la derecha del último bit 1 de un número.

void solve(){
  // be careful with typos and overflows, but also with the sizes of the arrays :::
   rd(n);
   fp(i, 1, n + 1) b[i] = 0, c[i] = 0;
   fp(i, 0, n){
    rd(a[i]);
    b[a[i]]++;
   }
   fp(i, 1, n + 1){
    if(b[i] > (n + 1) / 2){
      ps("-1");
      return;
    }
   }
   int var = 0;
   fp(i, 0, n - 1){
    if(a[i] == a[i + 1]) c[a[i]]++, c[a[i + 1]]++, var++;
   }
   c[a[0]]++, c[a[n - 1]]++;
   int mx = -1;
   fp(i, 1, n + 1) mx = max(mx, c[i]); 
   ps(var + max(0, mx - var - 2));
   return;
 } 
int main(){
cpu();
int __;
 cin>>__;
 while(__--){
  solve();
  // cout << nl;
 }
   // solve();
// cout<<nl;
 
return 0;
}