#include <bits/stdc++.h>
using namespace std;
int main() {
  int t;
  cin >> t;
  while (t--) {
    int n;
    cin >> n;
    vector<int> a(n);
    for (int i = 0; i < n; i++) cin >> a[i];
    bool flag = true;
    for (int i = 0; i < n; i++) {
      if (a[i] != i + 1) flag = false;
    }
    if (flag) {
      cout << 0 << endl;
      continue;
    }
    int l = 0, r = n - 1;
    for (int i = 0; i < n - 1; i++) {
      if (a[i] == i + 1)
        l++;
      else
        break;
    }
    for (int i = n - 1; i >= 0; i--) {
      if (a[i] == i + 1)
        r--;
      else
        break;
    }
    flag = false;
    for (int i = l; i <= r; i++) {
      if (a[i] == i + 1) flag = true;
    }
    if (flag)
      cout << 2 << endl;
    else
      cout << 1 << endl;
  }
}
