#include<iostream>
#include<bits/stdc++.h>
using namespace std;
#define ll long long int
#define _imax INT32_MAX
#define _imin INT32_MIN
#define mm 1000000007
#define pb push_back
struct node{
    int i,j,x,y;
};
int gcd(int a,int b){
    if(b==0){
        return a;
    }
    return gcd(b,a%b);
}
void solve(){
    int n;
    cin>>n;
    int a[n];
    for(int &i:a){
        cin>>i;
    }
    int f=0;
    for(int i=1;i<n;i++){
        if(a[i]<a[i-1]){
            f=1;
            break;
        }
    }
    if(f==0){
        cout<<0<<'\n';
    }else if(a[0]==1 || a[n-1]==n){
        cout<<1<<'\n';
    }else if(a[0]==n && a[n-1]==1){
        cout<<3<<'\n';
    }else{
        cout<<2<<'\n';
    }
}
int main(){
    int t;
    cin>>t;
    while(t>0){
        solve();
        t--;
    }
    return 0;
}