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

const int MAXN = 300000;
int t, n;
int a[MAXN+1];
int ans;
bool tf;
map <int,int> cnt, arr;

signed main() {
  ios_base::sync_with_stdio(0), cin.tie(0);
  cin >> t;
  while (t--) {
    cin >> n;
    ans = 0;
    tf = 1;
    cnt.clear();
    arr.clear();
    for (int i = 1; i <= n; i++) {
      cin >> a[i];
      cnt[a[i]]++;
    }
    int h = a[1];
    int c = 2;
    for (int i = 2; i <= n; i++) {
      if (a[i] == a[i-1]) {
        arr[h]++;
        arr[a[i-1]]++;
        h = a[i];
        c += 2;
        ans++;
      }
    }
    arr[h]++;
    arr[a[n]]++;
    int mmax = 0;
    for (auto it : arr)
      mmax = max(mmax, it.second);
    if (mmax > c / 2 + 1)
      ans += (mmax - (c / 2 + 1));
    for (auto it : cnt)
      if (it.second > (n+1)/2) tf = 0;
    if (tf) cout << ans << "\n";
    else cout << -1 << "\n";
  }
}
