#include<bits/stdc++.h>
using namespace std;
#define ll long long
#define pb push_back
#define mp make_pair
#define f(i,a,b) for(ll i=a;i<b;i++)
#define fm(it,um) for(auto it= um.begin();it!=um.end();it++)
#define vll vector<ll>
#define vi vector<int>
#define pii pair<int,int>
#define pli pair<ll,ll>
#define vpii vector<pair<int,int> > 
#define vpli vector<pair<ll,ll> > 
#define mii map<int,int> 
#define umii unordered_map<int,int>
#define pq priority_queue<int> //max_heap
#define pqi priority_queue<int,vector<int>,greater<int> > //min_heap
#define yes cout<<"YES"<<endl;
#define no cout<<"NO"<<endl;
#define all(x) x.begin(),x.end()
const ll MAX_SZ=2005;
ll dp[MAX_SZ][MAX_SZ];

void solve(){
    int n;
    cin>>n;
    vi V(n);
    f(i,0,n)
        cin>>V[i];
    int flag=0,a=1;
    f(i,0,n){
        if(V[i]!=a){
            flag=1;
            break;
        }
        a++;
    }
    if(flag==0){
        cout<<0<<endl;
        return;
    }
    else if(flag==1){
        if(V[0]==1){
            cout<<1<<endl;
        }
        else if(V[n-1]==1 && V[0]==n){
            cout<<3<<endl;
        }
        else if(V[n-1]==n){
            cout<<1<<endl;
        }
        else
            cout<<2<<endl;
    }
}

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