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

#define int long long
#define fs first
#define sc second
#define all(x) x.begin(), x.end()
#define rall(x) x.rbegin(), x.rend()
#define sz(x) (int)x.size()
#define pb push_back
#define fastio ios_base::sync_with_stdio(0), cin.tie(0), cout.tie(0)
#define MP make_pair
#define sqr(x) ((x) * (x))
mt19937 rng(chrono::steady_clock::now().time_since_epoch().count());

void solve() {
    int n;
    cin >> n;
    vector<int> a(n);
    for (int i = 0; i < n; i++) {
        cin >> a[i];
        a[i]--;
    }
    vector<int> cnt(n);
    for (int &x : a) {
        cnt[x]++;
    }
    if (*max_element(all(cnt)) * 2 > n + 1) {
        cout << "-1\n";
        return;
    }
    fill(all(cnt), 0LL);
    int ans = 0;
    for (int i = 1; i < n; i++) {
        if (a[i] == a[i - 1]) {
            cnt[a[i]] += 2;
            ans += 1;
        }
    }
    cnt[a[0]]++;
    cnt[a[n - 1]]++;
    ans += max(0LL, *max_element(all(cnt)) - ans - 2);
    cout << ans << '\n';
}

signed main() {
    fastio;
#ifdef FlameDragon
    freopen("in.txt", "r", stdin);
    freopen("out.txt", "w", stdout);
#endif
    int q;
    cin >> q;
    while (q--) {
        solve();
    }
}
