#include <bits/stdc++.h>
#include <math.h>
using namespace std;
int main()
{
    int t;
    cin>>t;
    
    while(t--){
   int n;
   cin>>n;
  int arr1[n];
  int arr2[n];
  for(int i=0;i<n;i++){
      cin>>arr1[i];
      arr2[i]=arr1[i];
  }
  sort(arr2,arr2+n);
  int c=1,b=0,a=0;
  for(int i=0;i<n;i++){
      if(
          arr2[i]!=arr1[i]
      ){
          c=0;
          break;
      }
  }
  if(arr1[0]==arr2[0]){
      b=1;
  }
  if(arr1[n-1]==arr2[n-1]){
      b=1;
  }
  if((arr1[0]==arr2[n-1])&&(arr1[n-1]==arr2[0])){
      a=1;
  }
  if(a==1){
      cout<<3<<endl;
  }
  else{
  if(c==1){
      cout<<0<<endl;
  }
  else{
      if(b==0){
          cout<<2<<endl;
      }
      else{
          cout<<1<<endl;
      }
  }
  }
    }
    

    return 0;
}