#include <bits/stdc++.h>
using namespace std;
int main() {
  int t;
  cin >> t;
  while (t--) {
    int n;
    cin >> n;
    vector<int> v(n);
    for (int i = 0; i < n; i++) {
      cin >> v[i];
    }
    int ans = 0;
    int check = 0;
    for (int i = 0; i < n; i++) {
      if (v[i] == i + 1) {
        check++;
      }
    }
    if (check == n) {
      cout << 0 << endl;
      continue;
    }
    if (check == 0) {
      cout << 1 << endl;
      continue;
    }
    int s = 0;
    while (s < n && v[s] == s + 1) {
      s++;
    }
    int e = n - 1;
    while (e >= 0 && v[e] == e + 1) {
      e--;
    }
    int f = 0;
    for (int i = s; i <= e; i++) {
      if (v[i] == i + 1) {
        f = 1;
        break;
      }
    }
    if (f || ((e - s + 1) == 1))
      cout << 2 << endl;
    else
      cout << 1 << endl;
  }
  return 0;
}
