//not-my-source-code
#include <iostream>
#include <cstdio>
using namespace std;
const int N=5010;
int f[N][N],minN[N][N],maxN[N][N],last[N];
int n,q,a[N];
void work()
{
int i,j,k,ii,jj;
for (i=1;i<=n;i++)
{
f[i][i]=1;
last[i]=i;
minN[i][i]=maxN[i][i]=a[i];
for (j=i+1;j<=n;j++)
{
minN[i][j]=min(minN[i][j-1],a[j]);
maxN[i][j]=max(maxN[i][j-1],a[j]);
}
}
for (k=2;k<=n;k++)
for (i=1;i<=n-k+1;i++)
{
j=i+k-1;
if (maxN[i][j]-minN[i][j]!=j-i)
f[i][j]=0;
else
{
if (minN[i][j]<minN[i][last[i]])f[i][j]=1;
else
f[i][j]=f[i][last[i]]+f[last[i]+1][j];
last[i]=j;
}
}
int ans=f[1][n];
for (i=1;i<=n;i++)
for (j=i;j<=n;j++)
if (f[i][j]&&(i==1||f[1][i-1]&&minN[1][i-1]==1))
{
jj=maxN[i][j];
if (jj==n||maxN[jj+1][n]==n&&f[jj+1][n])
for (ii=jj;ii>j;ii--)
if (f[ii][jj]&&minN[ii][jj]==i)
ans=max(ans,f[1][i-1]+f[j+1][ii-1]+f[jj+1][n]+2);
}
cout<<ans<<endl;
}
int main()
{
int T,i,j;
cin>>T;
for (i=1;i<=T;i++)
{
cin>>n>>q;
for (j=1;j<=n;j++)cin>>a[j];
printf("Case #%d: ",i);
work();
}
return 0;
}
Ly9ub3QtbXktc291cmNlLWNvZGUKCiNpbmNsdWRlIDxpb3N0cmVhbT4KI2luY2x1ZGUgPGNzdGRpbz4KdXNpbmcgbmFtZXNwYWNlIHN0ZDsKCmNvbnN0IGludCBOPTUwMTA7CmludCBmW05dW05dLG1pbk5bTl1bTl0sbWF4TltOXVtOXSxsYXN0W05dOwppbnQgbixxLGFbTl07Cgp2b2lkIHdvcmsoKQp7CglpbnQgaSxqLGssaWksamo7Cglmb3IgKGk9MTtpPD1uO2krKykKCXsKCQlmW2ldW2ldPTE7CgkJbGFzdFtpXT1pOwoJCW1pbk5baV1ba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