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

#define forsn(i, s, n) for(int i = int(s); i < int(n); ++i)
#define forn(i, n) forsn(i, 0, n)
#define dforsn(i, s, n) for(int i = int(n)-1; i >= int(s); --i)
#define dforn(i, n) dforsn(i, 0, n)

#define DBG(x) do{cout<<#x<<" = "<<x<<endl;}while(false)
template<class T> ostream & operator<<(ostream & out, vector<T> v){
    out<<"[ ";
    for(auto k : v) out<<k<<" ";
    out<<"]";
    return out;
}

#define SZ(v) (int((v).size()))

typedef long long int ll;

struct nei {
    int a = -1;
    int b = -1;
};

int main() {
	cin.tie(0); ios::sync_with_stdio(0);
    int t;
    cin >> t;
    while(t-->0) {
        int n;
        cin >> n;
        vector<int> ns(n);
        forn(i, n) {
            cin >> ns[i];
            ns[i]--;
        }
        vector<int> count(n, 0);
        int sum = 2;
        /*
        vector<nei> llist(n, nei{-1, -1});

        forn(i, n-1) {
            neis[i].a = i+1;
            neis[i+1].b = i;
        }

        vector<pair<int, int>> used;
        */

        count[ns[0]]++;
        count[ns.back()]++;
        forn(i, n-1) {
            if(ns[i] == ns[i+1]) {
                count[ns[i]] += 2;
                sum += 2;
            }
        }
        auto it = max_element(count.begin(), count.end());
        int k = int(distance(count.begin(), it));
        int amount = *it;
        for(int i = 0; 2*amount - 2 > sum && i < n-1; ++i) {
            if(ns[i] != k && ns[i+1] != k && ns[i] != ns[i+1]) {
                sum += 2;
            }
        }

        if(2*amount - 2 > sum) {
            cout << "-1\n";
        } else {
            cout << sum/2 - 1 << "\n";
        }
    }
}
