#include <stdio.h>
#include <vector>
#include <string>
#include <limits.h>
#include <queue>
#include <algorithm>
using namespace std;

#define ll long long

int main(){
	int tc; scanf("%d", &tc);

	for(int n; tc --;){
		scanf("%d", &n);
		vector<int> v(n);
		for(int i = 0; i < n; i ++)
			scanf("%d", &v[i]);

		vector<pair<int, int> > group, add;
		for(int i = 0; i < n; i ++){
			if(!group.empty() && v[i] != v[i - 1]){
				add.push_back({v[i - 1], v[i]});
				group.back().second = v[i];
			}else group.push_back({v[i], v[i]});
		}

		vector<int> cnt(n + 1, 0);
		for(pair<int, int> i : group){
			cnt[i.first] ++;
			cnt[i.second] ++;
		}

		int ans = group.size() - 1;
		int x = *max_element(cnt.begin(), cnt.end());
		if(x <= group.size() + 1){
			printf("%d\n", ans);
			continue;
		}

		int avoid = max_element(cnt.begin(), cnt.end()) - cnt.begin();
		bool found = false;

		for(pair<int, int> i : add){
			if(i.first != avoid && i.second != avoid){
				ans ++;
				group.push_back(i);
				if(x <= group.size() + 1){
					printf("%d\n", ans);
					found = true;
					break;
				}
			}
		}

		if(!found) printf("-1\n");
	}

	return 0;
}