
// Problem: F. The Struggling Contestant
// Contest: Codeforces - Codeforces Global Round 12
// URL: https://codeforces.com/contest/1450/problem/F
// Memory Limit: 256 MB
// Time Limit: 1000 ms
// Powered by CP Editor (https://github.com/cpeditor/cpeditor)

#include <bits/stdc++.h>
#define int long long
#define double long double
using namespace std;
 
inline int read()
{
    int s = 0, w = 1;
    char ch = getchar();
    while (ch < '0' || ch > '9')
    {
        if (ch == '-')
            w = -1;
        ch = getchar();
    }
    while (ch >= '0' && ch <= '9')
        s = s * 10 + ch - '0', ch = getchar();
    return s * w;
}
 
const int MAXN = 1e5 + 10;
const int INF = 0x3f3f3f3f;
const int MOD = 1e9 + 7;
const double eps = 1e-9;
const double PI = acos(-1.0);
 
int n, m, k;
 
int arr[MAXN];

int cnt[MAXN], num[MAXN];

signed main()
{
    // freopen("in.txt", "r", stdin);
    // freopen("out.txt", "w", stdout);
    int T = read();
    while (T--)
    {
        n = read();
        for (int i = 0; i <= n; i++)
        {
        	cnt[i] = num[i] = 0;
        }
        int k = 0;
        for (int i = 0; i < n; i++)
        {
            arr[i] = read();
            num[arr[i]]++;
            if (i && arr[i] == arr[i - 1])
            {
            	k++;
            	cnt[arr[i]] += 2;
            }
        }
        cnt[arr[0]]++, cnt[arr[n - 1]]++;
        int max_cnt = 0, max_num = 0;
        for (int i = 1; i <= n; i++)
        {
        	max_cnt = max(max_cnt, cnt[i]);
        	max_num = max(max_num, num[i]);
        }
        if (max_num * 2 > n + 1) cout << -1 << endl;
        else cout << k + max(0ll, max_cnt - (k + 2)) << endl;
    }
    return 0;
}