#include<bits/stdc++.h>
using namespace std;
using ll = long long;
int main() {
	ios::sync_with_stdio(false), cin.tie(nullptr);
	int _;
	for (cin >> _; _--;) {
		int n;
		cin >> n;
		int a[n + 1];
		int cnt = 0;
		for (int i = 1; i <= n; ++i) {
			cin >> a[i];
			if (a[i] != i)cnt++;
		}
		if (cnt == 0) cout << "0\n";
		else if (a[1] == 1 || a[n] == n)cout << "1\n";
		else {
			if (a[1] == n and a[n] == 1)cout << "3\n";
			else cout << "2\n";
		}
	}
	return 0;
}