#include <bits/stdc++.h>
#define ll long long
#define pb push_back
#define mp make_pair
using namespace std;
const ll infl = 1LL << 60;
int mod = 1e9 + 7;

/*int gcd(int a, int b)
{
	if (a < b)
	{
		swap(a, b);
	}
	if (a == 1 || b == 1)
	{
		return 1;
	}
	if (a == 0 || b == 0)
	{
		return max(a, b);
	}
	if (a == b)
	{
		return a;
	}
	else
	{
		return gcd(b, a % b);
	}
}*/

int main() {
	ios::sync_with_stdio(false);
	int t;
	cin >> t; int count = 0;
	while (t--)
	{
		int n; int flag = 0;
		cin >> n;
		int a[n];
		for (int i = 0; i < n; i++)
		{
			cin >> a[i];
		}
		int x1 = a[n - 1], x2 = n - 1, y1 = a[n - 1], y2 = n - 1;
		for (int i = 0; i < n - 1; i++)
		{
			if (a[i] > a[i + 1])
			{
				flag = 1;
			}
			if (a[i] > y1)
			{
				y1 = a[i]; y2 = i;
			}
			if (a[i] < x1)
			{
				x1 = a[i]; x2 = i;
			}
		}
		if (flag == 0)
		{
			cout << 0 << endl;
			continue;
		}
		if ((x2 == 0 || y2 == n - 1) && flag == 1)
		{
			cout << 1 << endl;
			continue;
		}
		if ((x2 == n - 1 && y2 == 0) && flag == 1)
		{
			cout << 3 << endl;
			continue;
		}
		cout << 2 << endl;
	}
}