//A
//如果两者的距离是奇数的话,那就追不上,如果是偶数,那就追的上
//因为偶数是与小明同类型的位置,只需要把小明逼到墙角就可以了
//可以画一个九宫格,然后把9x9的格子分两种颜色染上,一开始在相同颜色的格子一定会相遇
#include <bits/stdc++.h>
#define int long long
using namespace std;
const int N=1e6+10,mod=1e9+7;
string s;
int n,t,a[N],res,num,ans,m;
bool vis[N];
bool check(int u)
{
int x=0,step=0;
bool f=false,first=false;
for(int i=0;i<n;i++){
if(f&&a[i]!=u||!first&&a[i]!=u) x=max(x,++step);
else if(a[i]==u) f=!f,step=0,first=true;
}
if(res<=x) return false;
else{
res=x;
return true;
}
}
signed main()
{
std::ios::sync_with_stdio(false),cin.tie(0),cout.tie(0);
cin>>t;
while(t--){
cin>>n>>m;
res=0x3f3f3f;
for(int i=0;i<n;i++) cin>>a[i],ans=max(ans,a[i]);
int l=1,r=ans+1;
while(l<r){
int mid=l+r>>1;
if(check(mid)) r=mid;
else l=mid+1;
}
cout<<res<<endl;
}
return 0;
}
//B
//用lst数组来记录这个数字上一次出现的位置,然后把所有的情况统计到一个数组里面
//接下来分类讨论,对于当前数字的最大的距离,可以直接/2,然后比较/2之后的值与次大的距离,取最大
//然后取最小
#include <bits/stdc++.h>
#define int long long
using namespace std;
const int N=1e6+10,mod=1e9+7;
string s;
int n,t,res,num,ans,m;
bool vis[N];
signed main()
{
std::ios::sync_with_stdio(false),cin.tie(0),cout.tie(0);
cin>>t;
while(t--){
cin>>n>>m;
vector<int>a(n+1);vector<int>lst(m+1,-1);vector<vector<int>>f(m+1);
for(int i=0;i<n;i++){
cin>>a[i];
f[a[i]].push_back(i-1-lst[a[i]]);
lst[a[i]]=i;
}
ans=n;
for(int i=0;i<=m;i++){
f[i].push_back(n-1-lst[i]);
sort(f[i].begin(),f[i].end(),greater());
res=f[i][0]/2;
if(f[i].size()>1) res=max(res,f[i][1]);
ans=min(ans,res);
}
cout<<ans<<endl;
}
return 0;
}