#include <bits/stdc++.h>
#define ll long long int
//#define fast ios_base::sync_with_stdio(false);cin.tie(NULL);cout.tie(NULL)
using namespace std;

int main() {
//    fast;
  int t;
  cin>>t;
  for(int g=1;g<=t;g++)
  {
  	ll n,i,x=0,f=0;
	cin>>n;
	ll a[n];
	for(i=0;i<n;i++){
		cin>>a[i];
		if(a[i]!=i+1) 
		   f=1;
	}
	if(f==0)
	  cout<<0<<endl;
	else if(a[0]==n && a[n-1]==1)
	  cout<<3<<endl;
	else if(a[0]!=1 && a[n-1]!=n)
	  cout<<2<<endl;
	else
	  cout<<1<<endl;
	}
 return 0;
}
