#include using namespace std; using ll=long long; using P=pair; int main(){ ll t; cin>>t; while(t--){ ll n; cin>>n; vector a(n); for(ll i=0;i>a[i]; } vector> G(n); vector in(n,0); for(ll i=0;i v; for(ll j=-1;j<=1;j++){ v.push_back(P(a[(i+j+n)%n],(i+j+n)%n)); } sort(v.begin(),v.end()); for(ll j=0;j<2;j++){ G[v[j].second].push_back(v[j+1].second); in[v[j+1].second]++; } if(v[1].first==v[2].first){ G[v[2].second].push_back(v[1].second); in[v[1].second]++; } } for(ll i=0;i=2){ sort(G[i].begin(),G[i].end()); G[i].erase(unique(G[i].begin(),G[i].end())); } } queue que; vector seen(n,false); for(ll i=0;i