#include <cstdio>
#include <queue>
#include <algorithm>
#include <set>
using namespace std;

int main(){
	int t=1;
	scanf("%d", &t);
	while(t--){
		int n;
		scanf("%d", &n);
		vector <int> a(n);
		for(int& i:a)
			scanf("%d", &i), i--;
		vector <pair <int, int> > groups;
		vector <pair <int, int> > additionals;
		for(int i = 0;i < n;i ++){
			if(!groups.empty() and a[i]!=a[i-1]){
				additionals.emplace_back(a[i-1], a[i]);
				groups.back().second=a[i];
			}else{
				groups.emplace_back(a[i], a[i]);
			}
		}
		vector <int> counters(n, 0);
		for(auto& i:groups){
			counters[i.first]++;
			counters[i.second]++;
		}
		int ans=groups.size()-1;
		int x=*max_element(counters.begin(), counters.end());
		if(x<=groups.size()+1){
			printf("%d\n", ans);
			continue;
		}
		int avoid=max_element(counters.begin(), counters.end())-counters.begin();
		bool found=false;
		for(int i = 0;i < additionals.size();i ++){
			if(additionals[i].first != avoid and additionals[i].second != avoid){
				ans++;
				groups.push_back(additionals[i]);
				if(x<=groups.size()+1){
					printf("%d\n", ans);
					found=true;
					break;
				}
			}
		}
		if(!found){
			printf("-1\n");
		}
	}
}