#include <bits/stdc++.h>
#define f first
#define s second
#define fore(i,a,b) for(int i = (a), ThxMK = (b); i < ThxMK; ++i)
#define pb push_back
#define all(s) begin(s), end(s)
#define _ ios_base::sync_with_stdio(0);cin.tie(0);cout.tie(0);
#define sz(s) int(s.size())
#define ENDL '\n'
using namespace std;
typedef long double ld;
typedef long long lli;
typedef pair<lli,lli> ii;
typedef vector<lli> vi;
#define deb(x) cout << #x": " << (x) << endl;




void solve() {
   lli n; cin >> n;
   vi v(n); fore(i, 0, n) cin >> v[i];
   bool b = true;
   fore(i, 1, n) {
     if(v[i] < v[i - 1]) b = false;
   }
   if(b) {
     cout << 0 << ENDL;
     return;
   }
   if(v[0] == 1 or v[n - 1] == n) {
     cout << 1 << ENDL;
     return;
   }
   if(v[0] == n and v[n - 1] == 1) {
     cout << 3 << ENDL;
     return;
   }
   cout << 2 << ENDL;
}

int main(){ _
  lli t; cin >> t; while(t--)
  solve();
  return 0;
}
