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

#define ll long long
#define ar array
#define L 1001
#define si(x)	scanf("%d",&x)
#define sl(x)	scanf("%lld",&x)
#define ss(s)	scanf("%s",s)
#define pi(x)	printf("%d\n",x)
#define pl(x)	printf("%lld\n",x)
#define ps(s)	printf("%s\n",s)
#define fo(i,n) for(i=0;i<n;i++)
#define Fo(i,k,n) for(i=k;k<n?i<n:i>n;k<n?i+=1:i-=1)
#define pb push_back
#define mp make_pair
#define F first
#define S second
#define deb(x)	cout << #x << '=' << x << endl
#define all(x) x.begin(), x.end()
#define clr(x) memset(x, 0, sizeof(x))
#define sortall(x) sort(all(x))
#define tr(it, a) for(auto it = a.begin(); it != a.end(); it++)
#define PI 3.1415926535897932384626

void solve() {
	int n, x;
	si(n);
	int w = 0, first = 0, last = 0, notsorted = 0;

	si(x);
	first = x;
	w = x;
	int i = n;
	i -= 2;
	while(i--) {
		si(x);
		if (w > x) notsorted = 1;
		w = x;
	}
	si(x);
	if (w > x) notsorted = 1;
	last = x;
//	deb(first); deb(last);
	if (first == n && last == 1) {
		pi(3); return;
	}
	if (!notsorted) {
		pi(0); return;
	}
	if (first != 1 && last != n) {
		pi(2); return;
	} 
	pi(1);
	
}

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