#include <iostream>
#include <vector>
#include <algorithm>
using namespace std;


int main()
{
   long long t;
   cin>>t;
   while(t--)
   {
       long long n;
       vector<long long> a,sor;
       cin>>n;
       for(int i=0;i<n;i++)
       {
           int k;
           cin>>k;
           a.push_back(k);
       }
       sor=a;
       sort(sor.begin(),sor.end());
       if(a==sor)
        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";
   }
}
