#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
bool sorted(ll a[], ll n)
{
    for(ll i=0;i<n-1;i++)
    {
        if(a[i]>a[i+1])
        return false;
    }
    return true;
}
int main()
{
    ll t,n,i,j,k,l;
    cin>>t;
    while(t--)
    {
        cin>>n;
        ll a[n];
        for(i=0;i<n;i++)
        cin>>a[i];
        if(sorted(a,n))
        cout<<"0\n";
        else if(a[0]==n&&a[n-1]==1)
        cout<<"3\n";
        else if(a[0]==1||a[n-1]==n)
        cout<<"1\n";
        else 
        cout<<"2\n";
    }
}