#include <bits/stdc++.h>
using namespace std;
using namespace std;
int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
  cout.tie(NULL);
  int t;
  cin >> t;
  while (t--) {
    int n;
    cin >> n;
    int a[n];
    for (long long int i = 0; i < n; i++) {
      cin >> a[i];
    }
    bool bc = 1;
    for (long long int i = 0; i < n; i++) {
      if (a[i] != i + 1) {
        bc = 0;
        break;
      }
    }
    if (bc) {
      cout << 0 << endl;
    } else {
      int lp = 0;
      bool oh = 1;
      int rp = n - 1;
      while (a[lp] == lp + 1) {
        lp++;
      }
      while (a[rp] == rp + 1) {
        rp--;
      }
      for (int j = lp; j <= rp; j++) {
        if (a[j] == j + 1) {
          oh = 0;
          break;
        }
      }
      if (oh) {
        cout << 1 << endl;
      } else {
        cout << 2 << endl;
      }
    }
  }
  return 0;
}
