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

void solve() {
	int n;
	cin >> n;
	int arr[n], flag = 1;
	for(int i = 0; i < n; i++) {
		cin >> arr[i];
		if(i && arr[i-1] > arr[i])	flag = 0;
	}

	if(flag)	cout << 0 << endl;
	else if(arr[0] == 1 || arr[n-1] == n)	cout << 1 << endl;
	else if(arr[0] == n && arr[n-1] == 1)	cout << 3 << endl;
	else cout << 2 << endl;

}


signed main() {
	int t;
	cin >> t;
	while(t--)	solve();
}