#include <bits/stdc++.h>
using namespace std;
#define int  long long int
#define ll long long
#define vi vector <int>
#define endl "\n"
#define f(n) for(int i=0;i<n;i++)
#define vii vector <pair<int,int> >
#define rep(i,a,b) for (int i=a;i<b;i++)
#define repD(i,a,b) for (int i=a;i>=b;i--)
#define speed ios_base::sync_with_stdio(0);cin.tie(0);cout.tie(0)
#define gcd __gcd
#define init(i,k) memset(i,k,sizeof i)
#define pb push_back
#define lb lower_bound
#define ub upper_bound
#define setBits(x) __builtin_popcountll(x)
#define pii pair <int,int>
#define ff first
#define ss second
#define all(a) a.begin(),a.end()
#define N 1000005
#define mod 1000000007
/*void in(vector<int> &v,int n)
{
    f(n)
    {
        cin>>v[i];
    }
}*/



/*vector<int> prime(10000000,0);
vector<int>p;
void seive()
{
    prime[0]=prime[1]=1;
    for(int i=2;i*i<=1000000;i++)
    {
        if(prime[i]==0)
        {
            for(int j=i*i;j<=1000000;j+=i)
            {
                prime[j]=1;
            }
        }
    }
    for(int i=0;i<1000000;i++)
    {
        if(prime[i]==0)
        {
            p.push_back(i);
        }
    }
    return ;
}*/
/////////////////////////////////////////////////////
/*----------------------------------------------*/
/////////////////////////////////////////////////////
void solve()
{
    int n;
    cin>>n;
    vi v(n),a(n);
    f(n)
    {
        cin>>v[i];
        a[i]=v[i];
    }
    sort(all(a));
    int f=0;
    f(n)
    {
        if(a[i]!=v[i])
        {
            f=1;
            break;
        }
    }
    if(f==0)
    {
        cout<<0<<endl;
    }
    else
    {
        if(v[n-1]==1 && v[0]==n)
        {
            cout<<"3"<<endl;
        }
        else if(v[n-1]==n || v[0]==1)
        {
            cout<<"1"<<endl;
        }
        else
        {
            cout<<"2"<<endl;
        }
    }
    return ;
}
signed main()
{
    speed;
    int t=1;  
    cin>>t;
    for(int i=1;i<=t;i++)
    {
        //cout<<"Case #"<<i<<": ";
        solve();
    }
    return 0;
}