//#pragma GCC optimize("O3", "Ofast", "unroll-loops", "unswitch-loops", "fast-math", "no-stack-protector")
// #pragma GCC target("sse,sse2,sse3,ssse3,sse4,sse4.1,sse4.2,abm,mmx,avx,avx2,popcnt,tune=native")
// #pragma comment(linker, "/STACK:66777216")

#include <bits/stdc++.h>

using namespace std;


// #include <ext/pb_ds/assoc_container.hpp>
// #include <ext/pb_ds/priority_queue.hpp>

// using namespace __gnu_pbds;

/** Begin fast allocation */
/*
const int mx_MEM = 2*1e8;
int mpos = 0;
char mem[mx_MEM];

inline void *operator new(size_t n) {
    assert((mpos += n) <= mx_MEM);
    return (void *) (mem + mpos - n);
}

inline void operator delete(void *) noexcept {} // must have!
*/
/** End fast allocation */
#define ll long long

int n;
vector<int> ms;


signed main() {
#ifdef _LOCAL
    freopen("input.txt", "r", stdin);
    freopen("output.txt", "w", stdout);
#endif
    ios_base::sync_with_stdio(false);
    cin.tie(0);
    cout.tie(0);
    int t;
    cin >> t;
    for (int i = 0; i < t; ++i) {
        cin >> n;
        ms.clear();
        for (int j = 0; j < n; ++j) {
            int x;
            cin >> x;
            ms.push_back(x - 1);
        }
        int ans = 0;
        vector<int> p(n, 0);
        for (int j = 0; j < n; ++j) {
            if (j == 0 || ms[j - 1] == ms[j]) {
                p[ms[j]]++;
            }
            if (j == n - 1 || ms[j] == ms[j + 1]) {
                p[ms[j]]++;
                ans++;
            }
        }

        int k = 0;
        int sm = 0;
        int mx = 0;
        for (int j = 0; j < n; ++j) {
            sm += p[j];
            if (p[j] > mx) {
                mx = p[j];
                k = j;
            }
        }
        sm -= mx;
        int r = sm - mx + 2;
        int uk = 0;
        while (uk != n-1 && r < 0) {
            if (ms[uk] != k && ms[uk + 1] != k && ms[uk] != ms[uk + 1]) {
                r += 2;
                ans++;
            }
            uk++;
        }
        if (r < 0) cout << -1;
        else cout << ans-1;
        cout << "\n";
    }
}