#include<bits/stdc++.h>
using namespace std;
#define endl "\n"
#define gc getchar_unlocked
#define fo(i,n) for(i=0;i<n;i++)
#define Fo(i,k,n) for(i=k;k<n?i<n:i>n;k<n?i+=1:i-=1)
#define ll long long
#define si(x) scanf("%d",&x)
#define sl(x) scanf("%lld",&x)
#define ss(s) scanf("%s",s)
#define pi(x) printf("%d\n",x)
#define pl(x) printf("%lld\n",x)
#define ps(s) printf("%s\n",s)
#define deb(x) cout << #x << "=" << x << endl
#define deb2(x, y) cout << #x << "=" << x << "," << #y << "=" << y << endl
#define pb push_back
#define mp make_pair
#define F first
#define S second
#define all(x) x.begin(), x.end()
#define clr(x) memset(x, 0, sizeof(x))
#define sortall(x) sort(all(x))
#define tr(it, a) for(auto it = a.begin(); it != a.end(); it++)
#define PI 3.1415926535897932384626
typedef pair<int, int> pii;
typedef pair<ll, ll> pl;
typedef vector<int> vi;
typedef vector<ll> vl;
typedef vector<pii> vpii;
typedef vector<pl> vpl;
typedef vector<vi> vvi;
typedef vector<vl> vvl;
const int mod = 1000000007;
const int N = 2e5+10, M = N;
int main() {
ios_base::sync_with_stdio(false);
cin.tie(NULL);
int t,x;
si(t);
fo(x,t)
{
int i, k, p, q, r, x, y, u, v, m=0, n, j;
si(n);
int arr[n+2];
map<pair<int,int>,int>mp;
fo(i,n){
si(arr[i]);
m=max(m,arr[i]);
}
int pref[n+2][m+2],cnt[m+2];
fo(i,m+1){
cnt[i]=0;
}
fo(i,n){
fo(j,m+1){
pref[i][j]=0;
}
}
fo(i,n){
cnt[arr[i]]++;
}
pref[0][arr[0]]=1;
mp[{1,arr[0]}]=1;
Fo(i,1,n){
Fo(j,1,m+1){
if(j == arr[i]){
pref[i][j]=1;
}
pref[i][j]+=pref[i-1][j];
if(mp[{pref[i][j],j}] == 0)
mp[{pref[i][j],j}]=i+1;
}
}
int gloans=1,prev=0,ans,reqdcount,idx,maxi;
fo(i,n-1){
ans=1;
j=arr[i];
reqdcount=cnt[j]-pref[i][j]+1;
idx=mp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dW2FyclswXV09MTsKICAgIAltcFt7MSxhcnJbMF19XT0xOwogICAgCUZvKGksMSxuKXsKICAgIAkJRm8oaiwxLG0rMSl7CiAgICAJCQlpZihqID09IGFycltpXSl7CiAgICAJCQkJcHJlZltpXVtqXT0xOwkKICAgIAkJCX0KICAgIAkJCXByZWZbaV1bal0rPXByZWZbaS0xXVtqXTsKICAgIAkJCWlmKG1wW3twcmVmW2ldW2pdLGp9XSA9PSAwKQogICAgCQkJCW1wW3twcmVmW2ldW2pdLGp9XT1pKzE7CiAgICAJCX0KICAgIAl9CiAgICAJaW50IGdsb2Fucz0xLHByZXY9MCxhbnMscmVxZGNvdW50LGlkeCxtYXhpOwogICAgCWZvKGksbi0xKXsKICAgIAkJIGFucz0xOwogICAgCQkJaj1hcnJbaV07CiAgICAJCQlyZXFkY291bnQ9Y250W2pdLXByZWZbaV1bal0rMTsKICAgIAkJCWlkeD1tcFt7cmVxZGNvdW50LGp9XS0xOwogICAgCQkJaWYoaWR4PmkpewogICAgCQkJCWFucz0yKnByZWZbaV1bal07CiAgICAJCQkJcHJldj1hbnM7CiAgICAJCQkJbWF4aT0wOwogICAgCQkJCUZvKGssMSxtKzEpewogICAgCQkJCQltYXhpPW1heChtYXhpLHByZWZbaWR4LTFdW2tdLXByZWZbaV1ba10pOwogICAgCQkJCX0KICAgIAkJCQlhbnMrPW1heGk7CiAgICAJCQkJZ2xvYW5zPW1heChnbG9hbnMsYW5zKTsKICAgIAkJCX0KICAgIAl9CiAgICAJY291dDw8Z2xvYW5zPDwiXG4iOwogICAgfQp9