#include <bits/stdc++.h>
using namespace std;
int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(0);
  cout.tie(0);
  int t;
  cin >> t;
  for (int T = 0; T < t; T++) {
    int n;
    cin >> n;
    vector<int> ansss;
    int min1 = n - 1;
    int max1 = 0;
    for (int i = 0; i < n; i++) {
      int temp;
      cin >> temp;
      if (temp != i + 1) {
        if (min1 > i) {
          min1 = i;
        }
        if (max1 < i) {
          max1 = i;
        }
      }
      ansss.push_back(temp);
    }
    if (min1 == n - 1 && max1 == 0) {
      cout << "0"
           << "\n";
    } else {
      bool flag = true;
      for (int ii = min1; ii <= max1; ii++) {
        if (ansss[ii] == ii + 1) {
          flag = false;
        }
      }
      if (flag) {
        cout << "1"
             << "\n";
      } else {
        cout << "2"
             << "\n";
      }
    }
  }
}
