#include <bits/stdc++.h>
using namespace std;
#define int long long
typedef pair<int,int> pi;
#define f first
#define s second
#define FAST ios_base::sync_with_stdio(0); cin.tie(0);
#define all(x) x.begin(),x.end()
typedef pair<int, pi> pii;

const int maxn = 210;
const int mod = 998244353;
const int INF = INT_MAX/2;

void solve() {
	int n; cin >> n;
	vector <int> v(n), cnt(n);
	
	for (int i =0;i<n;i++) {
		cin >> v[i];
		--v[i];
		cnt[v[i]]++;
	}
	
	//If max F(x) > ceil(n/2)
	if (*max_element(all(cnt)) * 2 > n + 1) {
		cout << "-1\n";
		return;
	}
	
	vector <int> endp(n);
	int k = 0;
	//cut betwen all adjacent with equal tag
	for (int i =0;i<n-1;i++) {
		if (v[i] == v[i+1]) {
			k++;
			endp[v[i]]++;
			endp[v[i+1]]++;
		}
	}
	
	endp[v[0]]++;
	endp[v[n-1]]++;
	
	//Now we can reorder or rearrange each segment.
	//It is possible to prevent endpoints of adjacent segments from touching
	//If f(x) <= k + 2, since there are k+2 possible distinct endpoints that aren't touching.
	//Otherwise, exactly one f(x) will be > k + 2. We muts now form new cuts to increase k (possible endpoints)
	//until all f(x) <= k + 2.
	
	int mostf = *max_element(endp.begin(),endp.end());
	cout << k + max(0ll, mostf - k - 2) << "\n";
}


int32_t main() {
	FAST
	int tc; cin >> tc;
	while (tc--) solve();
}







