#include<iostream>
#include<vector>
#include<set>
#include<algorithm>
#include<stack>
#include<queue>
#include<string>
#include<map>
#include<cmath>
#include<string.h>
#include<math.h>
#include<unordered_map>
#include<iomanip>
 

// typedef long long int lli;
// typedef unsigned long long int ulli;
// typedef vector<int> vi;
// typedef vector<vi> vvi;
// typedef vector<lli> vll;
// typedef pair<int,int> pii;
// typedef pair<long,long> pll;
// typedef vector<pii> vpii;
// typedef vector<pll> vpll;


// #define ulli unsigned long long int
#define rep(i,x,n) for(int i=x;i<n;i++)
#define f first
#define s second
#define mod 1000000007
#define pb push_back
#define in insert
#define pres(c,x) ((c).find(x)!=c.end());

#define fast ios_base::sync_with_stdio(false);cin.tie(NULL);cout.tie(NULL)

using namespace std;

void solve();



int main(){
        fast;

        int t=1;
        cin>>t;

        while(t--){
            solve();
        }

    return 0;
}



void solve(){
int n;
cin>>n;
int a[n+1];
int k=0,z=0;
for(int i=1;i<=n;i++){
    cin>>a[i];
    if(a[i]==i){
        k++;
    }
    if(a[i]==n-i+1){
        z++;
    }
}
if(k==n){
    cout<<0<<endl;
    return;
}
if(a[1]==n&&a[n]==1){
    cout<<3<<endl;
    return;
}
 
if(a[1]==1||a[n]==n){
    cout<<1<<endl;
    return;
}else{
    cout<<2<<endl;
}
 
 
}