#include<bits/stdc++.h>
using namespace std;
template<typename T> void read(T &x)
{
	long long f=1;x=0;char c=getchar();
	while(!isdigit(c)){if(c=='-') f=-1;c=getchar();}
	while(isdigit(c)){x=(x<<1)+(x<<3)+c-48;c=getchar();}
	x*=f;
}
int T,n,a[100],ans,st,ji,h[100],nf,nn;
int main()
{
	read(T);
	while(T--)
	{
		read(n);st=0;ans=0;ji=0;nf=nn=0;
		memset(h,0,sizeof(h));
		for(int i=1;i<=n;i++) read(a[i]);
		for(int i=1;i<=n;i++)
		{
			if(a[i]!=i) ji=1;
			if(a[i]==1) nf=i;
			if(a[i]==n) nn=i;
		}
		if(ji==0) cout<<0<<endl;
		else
		{
			if(nf==1||nn==n) cout<<1<<endl;
			else 
			{
				if(nf==n&&nn==1) cout<<3<<endl;
				else cout<<2<<endl;
			}
		}
	}
	return 0;
}

