#include<bits/stdc++.h>
#define ll long long
#define ull unsigned long long
#define PI 3.14159265358979323846
#define IO ios::sync_with_stdio(0);cin.tie(NULL);cout.tie(NULL);
#define pb push_back
using namespace std;

const ll mod = 5000000;
const ll inf = 1e18 + 5;
const ll N = 100005;
const ll LG = 19;


ull gcd(ull a,ull b) { return b?gcd(b,a%b):a;}
ll n, k, a;


int main()
{//IO
#ifndef ONLINE_JUDGE
	freopen("out.txt","w",stdout);
	freopen("in.txt","r",stdin);
#else
#endif

	int t;
	cin>>t;
	while(t--){
		int n;cin>>n;
		int a[n], b[n], cnt = 0;
		for(int i = 0 ; i < n ; i++) cin>>a[i], b[i] = a[i];
		sort(b, b + n);
		for(int i = 0 ; i < n ; i++){
			if(a[i] != b[i]) cnt++;
		}
		if(cnt == 0) cout << 0 << endl;
		else{
			if(a[0] == b[0] || a[n - 1] == b[n - 1])cout << 1 << endl;
			else if(a[0] == b[n - 1] && a[n - 1] == b[0])cout << 3 << endl;
			else cout << 2 << endl;
		}
	}



    return 0;
}
