#include <bits/stdc++.h>
using namespace std;
const char ln = '\n';
const long long mod = 998244353;
const long double pi = 3.14159265359;
const long long N = 2e6 + 1;
const long long M = 1e9 + 7;
int main() {
  ios_base::sync_with_stdio(false), cin.tie(0), cout.tie(0);
  srand(time(NULL));
  ;
  long long t;
  cin >> t;
  while (t--) {
    long long n, k, sum = 0, ans = 0, flag = 0;
    cin >> n;
    vector<long long> a(n), b(n);
    for (long long i = 0; i < n; i++) {
      cin >> a[i];
      b[i] = a[i];
    }
    sort(b.begin(), b.end());
    if (a == b) {
      cout << 0 << ln;
      continue;
    }
    long long x, y;
    for (long long i = 0; i < n; i++) {
      if (a[i] != i + 1) {
        x = i;
        break;
      }
    }
    for (long long i = n - 1; i >= 0; i--) {
      if (a[i] != i + 1) {
        y = i;
        break;
      }
    }
    long long f = 0;
    if (x <= y) {
      for (long long i = x; i < y + 1; i++) {
        if (a[i] == i + 1) f = 1;
      }
    }
    if (f)
      cout << 2;
    else
      cout << 1;
    cout << ln;
  }
  return 0;
}
