#include <bits/stdc++.h>
using namespace std;
int main() {
  int t;
  cin >> t;
  while (t--) {
    int n;
    cin >> n;
    int x;
    bool a = false;
    int t = 0;
    int fb = 0;
    int fe = 0;
    for (int i = 0; i < n; i++) {
      cin >> x;
      if (x != i + 1) {
        a = true;
        fe = 0;
      }
      if (a == false && x == i + 1) {
        fb++;
        t++;
      }
      if (a == true && x == i + 1) {
        t++;
        fe++;
      }
    }
    if (t == n) {
      cout << "0" << '\n';
      continue;
    }
    if (t - fe - fb > 0) {
      cout << 2 << '\n';
    } else {
      cout << 1 << '\n';
    }
  }
}
