#include <bits/stdc++.h>
using namespace std;
int main() {
  int t;
  cin >> t;
  while (t--) {
    int n;
    cin >> n;
    vector<int> vs(n);
    for (int& a : vs) cin >> a;
    long long badPos = 0, contiguos = 0;
    bool start = false;
    for (int i = 0; i < n; i++) {
      if (i + 1 == vs[i]) {
        badPos++;
        if (start) {
          contiguos++;
          start = false;
        }
      }
      if (i + 1 != vs[i]) start = true;
    }
    if (start) contiguos++;
    if (is_sorted(vs.begin(), vs.end()))
      cout << 0 << '\n';
    else if (badPos == 0 || contiguos == 1)
      cout << 1 << '\n';
    else
      cout << 2 << '\n';
  }
  return 0;
}
