You are given an array aa of nn elements. You can apply the following operation to it any number of times:
- Select some subarray from aa of even size 2k2k that begins at position ll (1≤l≤l+2⋅k−1≤n1≤l≤l+2⋅k−1≤n, k≥1k≥1) and for each ii between 00 and k−1k−1 (inclusive), assign the value al+k+ial+k+i to al+ial+i.
For example, if a=[2,1,3,4,5,3]a=[2,1,3,4,5,3], then choose l=1l=1 and k=2k=2, applying this operation the array will become a=[3,4,3,4,5,3]a=[3,4,3,4,5,3].
Find the minimum number of operations (possibly zero) needed to make all the elements of the array equal.
Input
The input consists of multiple test cases. The first line contains a single integer tt (1≤t≤2⋅1041≤t≤2⋅104) — the number of test cases. Description of the test cases follows.
The first line of each test case contains an integer nn (1≤n≤2⋅1051≤n≤2⋅105) — the length of the array.
The second line of each test case consists of nn integers a1,a2,…,ana1,a2,…,an (1≤ai≤n1≤ai≤n) — the elements of the array aa.
It is guaranteed that the sum of nn over all test cases does not exceed 2⋅1052⋅105.
Output
Print tt lines, each line containing the answer to the corresponding test case — the minimum number of operations needed to make equal all the elements of the array with the given operation.
Example
input
Copy
5 3 1 1 1 2 2 1 5 4 4 4 2 4 4 4 2 1 3 1 1
output
Copy
0 1 1 2 0
Note
In the first test, all elements are equal, therefore no operations are needed.
In the second test, you can apply one operation with k=1k=1 and l=1l=1, set a1:=a2a1:=a2, and the array becomes [1,1][1,1] with 11 operation.
In the third test, you can apply one operation with k=1k=1 and l=4l=4, set a4:=a5a4:=a5, and the array becomes [4,4,4,4,4][4,4,4,4,4].
In the fourth test, you can apply one operation with k=1k=1 and l=3l=3, set a3:=a4a3:=a4, and the array becomes [4,2,3,3][4,2,3,3], then you can apply another operation with k=2k=2 and l=1l=1, set a1:=a3a1:=a3, a2:=a4a2:=a4, and the array becomes [3,3,3,3][3,3,3,3].
In the fifth test, there is only one element, therefore no operations are needed.
#include<iostream>
#include<cstdio>
#include<cmath>
#include<queue>
#include<vector>
#include<algorithm>
#include<cstring>
using namespace std;
inline int read(){
int x=0,f=1;
char ch=getchar();
while(ch<'0'||ch>'9'){
if(ch=='-')f=-1;
ch=getchar();
}
while(ch>='0'&&ch<='9'){
x=(x<<1)+(x<<3)+(ch^48);
ch=getchar();
}
return x*f;
}
const int N=2e5+10;
int a[N];
int main(){
int t;
t=read();
while(t--){
int n;
n=read();
for(int i=1;i<=n;++i){
a[i]=read();
}
int cnt=0;
int num=n;
for(int i=n;i>0;--i){
if(a[i]==a[n]){
++cnt;--num;
}else break;
}
if(num==0){
printf("0\n");
continue;
}
int flag=0;
for(int i=1;i<=n;++i){
if(a[i]==a[n])flag++;
else break;
}
int ans=0;
while(num-flag>0){
ans++;
num-=cnt;
cnt*=2;
if(num<=0)break;
if(num-flag<=0)break;
while(a[num]==a[n]){
++cnt;
--num;
}
if(num<=0)break;
}
printf("%d\n",ans);
}
return 0;
}