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

#define endl '\n'
#define ll long long 
#define fi first
#define se second
#define pb push_back

#define nl cout << endl
#define all(x) x.begin(), x.end()
#define rep(i, a, b) for(int i = a; i < b; i++)
#define repn(i, n) rep(i, 0, n)
#define deb(x) cout << (#x)  << " " << (x) << endl
#define deb2(x, y) cout << (#x)  << " " << (x) << " " << (#y) << " " << y << endl
#define deb3(x, y, z) cout << (#x)  << " " << (x) << " " << (#y) << " " << y << " " << (#z) << " " << z << endl

#define pii pair <int, int> 
#define vi vector <int> 
#define mkp make_pair
#define found(x, y) x.find(y) != x.end()

#define INF 1000000007
#define N 100005

template <typename... T>
void debug(T... args) {
	((cerr << args << " "), ...);
}

template <typename... T>
void read(T&...  args) {
	((cin >> args), ...);
}

template <typename... T> 
void write(T... args) {
	((cout << args << " "), ...);
}

bool isPalindrome(string s) {
	string t = s; 
	reverse(all(t)); 
	return t == s; 
}

void solve(){
	
	int n;
	read(n); 
	
	int a[n]; 
	repn(i, n) {
		read(a[i]); 
	}

	bool valid = is_sorted(a, a+n); 
	if(valid) {
		cout << 0 << endl; 
		return ; 
	}

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

}
 
int main(){

	
	ios_base:: sync_with_stdio(false);
	cin.tie(0);
	cout.tie(0);

	int t = 1;
	read(t);
	
	while(t--){
		solve();
	}
	
	return 0;
}
