#include<bits/stdc++.h>
#define ll long long
using namespace std;



int main(){
	ios_base::sync_with_stdio(false);
	cin.tie(NULL);
	int t,n; cin>>t;
	while(t--){
		cin>>n;
		int a[n];
		for(int i=0; i<n; i++) cin>>a[i];
		if(is_sorted(a,a+n)) {cout<<0<<endl;  continue;}
		if(a[0] == 1 && a[n-1] == n) {cout<<1<<endl;  continue;}
		if(a[0] == n && a[n-1] == 1) {cout<<3<<endl;  continue;}
		if(a[0] == 1 && a[n-1] != n) {cout<<1<<endl;  continue;}
		if(a[0] != 1 && a[n-1] == n) {cout<<1<<endl;  continue;}		
		if(a[0] == n && a[n-1] != 1) {cout<<2<<endl;  continue;}
		if(a[0] != n && a[n-1] == 1) {cout<<2<<endl;  continue;}
		if(a[0] != 1 && a[n-1] != n) {cout<<2<<endl;  continue;}
	}
	return 0;	
}