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

void ng() { cout << -1 << "\n"; }

void solve() {
    int N; cin >> N;
    map<int, int> M;
    vector<int> A(N);
    for (auto &x: A) {
        cin >> x; M[x]++;
    }
    for (auto &[key, val]: M) {
        if (val > (N + 1) / 2) return ng();
    }
    int l = 0;
    vector<pair<int, int>> V;
    for (int i = 0; i < N-1; i++) {
        if (A[i] == A[i+1]) {
            V.emplace_back(A[l], A[i]);
            l = i+1;
        }
    }
    V.emplace_back(A[l], A[N-1]);
    M.clear();
    int sum = V.size();
    int sum2 = 0;
    map<int, int> P;
    for (auto &[c, d]: V) {
        if (c == d) P[c]++, sum2++;
        else M[c]++, M[d]++;
    }
    sum -= sum2;
    for (auto &[key, val]: P) {
        if (sum - M[key] + 1 + (sum2 - val) < val) {
            int a = val - (sum - M[key] + 1 + (sum2 - val));
            cout << V.size() - 1 + a << "\n";
            return;
        }
    }
    cout << V.size() - 1 << "\n";
}

int main() {
    cin.tie(nullptr);
    ios::sync_with_stdio(false);
    int T; cin >> T;
    while (T--) solve();
    return 0;
}