#include <iostream>
#include <chrono>
#include <map>
#include <random>
#include <set>
#include <unordered_map>
#include <unordered_set>
#include <fstream>
#include <cstring>

//using namespace std;

typedef long long ll;
typedef long double ld;

const ll OO = 2e18 + 20;
const int oo = 1e9 + 10;
const int N = 1e5 + 5;
const int MOD = 1e9 + 7;

std::mt19937 rng(std::chrono::steady_clock::now().time_since_epoch().count());
//int randi = uniform_int_distribution<int>(0, 999)(rng);

struct Seg {
    int l, r;
};

int n;
int a[N], cnt[N];
std::vector<Seg> segs;

void solve() {
    std::cin >> n;
    for (int i = 0; i < n; i++) {
        std::cin >> a[i];
    }

    segs.clear();
    int fi = 0;
    for (int i = 1; i < n; i++) {
        if (a[i] == a[i - 1]) {
            segs.push_back({fi, i - 1});
            fi = i;
        }
    }
    segs.push_back({fi, n - 1});

    for (int i = 1; i <= n; i++) {
        cnt[i] = 0;
    }
    int mx = 1;
    for (auto &s : segs) {
        if (a[s.l] == a[s.r]) {
            cnt[a[s.l]]++;
            if (cnt[a[s.l]] > cnt[mx])
                mx = a[s.l];
        }
    }

    int other = 0;
    for (auto &s : segs) {
        if (a[s.l] != mx && a[s.r] != mx)
            other++;
    }

    if (cnt[mx] - other <= 1) {
        std::cout << segs.size() - 1 << "\n";
        return;
    }

    int rem = cnt[mx] - other - 1, cuts = 0;
    for (auto &s : segs) {
        for (int i = s.l + 1; i <= s.r; i++) {
            if (a[i - 1] != mx && a[i] != mx)
                cuts++;
        }
    }

    if (cuts >= rem) {
        std::cout << segs.size() - 1 + rem << "\n";
    } else {
        std::cout << -1 << "\n";
    }


}

int main() {
    std::ios::sync_with_stdio(false);std::cin.tie(nullptr);std::cout.tie(nullptr);
    std::chrono::steady_clock::time_point begin = std::chrono::steady_clock::now();

    int tests = 1;
    std::cin >> tests;
    while (tests--) {
        solve();
    }

    std::chrono::steady_clock::time_point end = std::chrono::steady_clock::now();
//    std::cout << "time in micros: " << std::chrono::duration_cast<std::chrono::microseconds>(end - begin).count();
    return 0;
}
