#include <vector>
#include <algorithm>
#include <numeric>
#include <iostream>
using namespace std;
 
template<typename T>std::istream&operator>>(std::istream&i,vector<T>&t) {for(auto&v:t){i>>v;}return i;}

class FTheStrugglingContestant {
public:
    void solve(istream& cin, ostream& cout) {
        int T; cin >> T;
        for (int t = 0; t < T; ++t) {
            int N; cin >> N;
            vector<int> A(N); cin >> A;
            if (N == 1) {
                cout << 0 << endl;
                continue;
            } else if (N == 2) {
                cout << -(A[0]==A[1]) << endl;
                continue;
            }
 
            for (int&a: A) --a;
            vector<int> C(N, 0), G(N, 0);
            for (int i = 0; i < N; ++i) {
                int c = 0;
                c += (i == 0 || A[i-1] == A[i]);
                c += (i == N-1 || A[i] == A[i+1]);
                C[A[i]] += c;
                G[A[i]]++;
            }
            int g = *max_element(G.begin(),G.end());
            if (2*g-1 > N) {
                cout << -1 << endl;
                continue;
            }
 
            int tot = accumulate(C.begin(),C.end(),0);
            int mx = *max_element(C.begin(),C.end());
            cout << max({mx-2, tot/2-1}) << '\n';
        }
    }
};
 
 
 
int main() {
	ios_base::sync_with_stdio(false); cin.tie(nullptr); cout.tie(nullptr);
	FTheStrugglingContestant solver;
	std::istream& in(std::cin);
	std::ostream& out(std::cout);
	solver.solve(in, out);
    return 0;
}