#include<bits/stdc++.h>

using namespace std;
int mod = 1000000007;

void solve(){
	int n;
	cin>>n;
	vector<int> a(n);
	for(int i=0;i<n;i++)
		cin>>a[i];
	int i=1;
	while(i<n && a[i]>=a[i-1])
		i++;
	if(i==n){
		cout<<0<<"\n";
		return;
	}
	int m1 = *min_element(a.begin(),a.end());
	int m2 = *max_element(a.begin(),a.end());
	if(a[0] == m1 || a[n-1] == m2)
		cout<<"1\n";
	else if(a[0] == m2 && a[n-1]==m1)
		cout<<"3\n";
	else
		cout<<"2\n";
}

int main(){
	ios_base::sync_with_stdio(false);
	cin.tie(NULL);
	int t;
	cin>>t;
	while(t--){
		solve();
	}

	return 0;
}