#include <bits/stdc++.h>
#include <algorithm>
#include <queue>
#include <vector>

//#define int long long int
#define inf 1e16
#define TC(t) while (t--)
#define forn(i, n) for (int i = 0; i < (int)(n); ++i)
using namespace std;


int32_t main()
{
   int t;
   cin>>t;
   while(t--)
   {
       int n;
       cin>>n;
       int arr[n];
       forn(i,n)
       cin>>arr[i];
       int brr[n];
       forn(i,n)
       brr[i]=arr[i];
       sort(brr,brr+n);
       if(is_sorted(arr,arr+n))
        cout<<0<<endl;
       else if(brr[n-1]==arr[n-1])
        cout<<1<<endl;
       else if(brr[0]==arr[0])
        cout<<1<<endl;
       else
        {
            if(arr[0]==brr[n-1] && arr[n-1]==brr[0])
                    cout<<3<<endl;
                else
                    cout<<2<<endl;
        }
   }

    return 0;
}
