#include<bits/stdc++.h>
typedef long long ll;
using namespace std;
#define ll long long
#define rep(j,a,b) for(ll j = a; j < b; j++)
#define all(v) v.begin(),v.end()
#define FF first
#define SS second
#define pb push_back
#define mp make_pair
typedef pair<ll, ll> pii;
typedef vector<ll> vi;
#define mod 1000000000

int main(){
  
ios_base::sync_with_stdio(0);
  cin.tie(0);
  

  ll t=1; 
    cin>>t;
  

while(t--)
{
 ll n; cin>>n;
 ll arr[n];

 rep(j,0,n)
  cin>>arr[j];

  if(is_sorted(arr,arr+n)) {cout<<0<<endl; continue;}

  if(arr[0] == 1 || arr[n-1] == n) cout<<1<<endl;
  else if(arr[0] == n && arr[n-1] == 1) cout<<3<<endl;
  else cout<<2<<endl;
    



} 
}




  
 



