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

typedef long long ll;
typedef long double ld;
typedef vector<ll> vi;
typedef pair<ll, ll> pii;
typedef vector<pii> vii;
typedef vector<vi> vvi;
const int MOD = 1e9 + 7;

#define fastIO \
ios_base::sync_with_stdio(false); cin.tie(nullptr);
#define endl '\n'
#define pb push_back
#define ff first
#define ss second
#define mp make_pair 

int main() {
    fastIO;
    int tt;
    cin >> tt;
    while (tt--) {
        int n;
        cin >> n;
        vi a(n);
        for (auto& i : a)    cin >> i;
        vi b(n + 1);
        for (auto i: a) b[i]++;
        if (*max_element(b.begin(), b.end()) > (n + 1) / 2) cout << -1 << endl;
        else {
            vi f(n + 1);
            int ans = 0;
            for (int i = 0; i < n - 1; i++) {
                if (a[i] == a[i + 1]) {
                    f[a[i]]++;
                    f[a[i + 1]]++;
                    ans++;
                }
            }
            f[a[0]]++;
            f[a[n - 1]]++;
            ans += max(0ll, *max_element(f.begin(), f.end()) - (ans + 2));
            cout << ans << endl;
        }
    }
    
    return 0;
}
    