#include <bits/stdc++.h>
using namespace std;
void stand8th() {
  long long n;
  cin >> n;
  long long a[n];
  for (long long i = 0; i < n; ++i) {
    cin >> a[i];
  }
  vector<long long> v;
  for (long long i = 0; i < n; ++i) {
    if (a[i] != i + 1) {
      v.push_back(i);
    }
  }
  if ((long long)(v.size()) == 0) {
    cout << 0 << endl;
    return;
  }
  long long fl = 1;
  for (long long i = 0; i < (long long)(v.size()) - 1; ++i) {
    if (v[i] + 1 != v[i + 1]) {
      fl = 0;
      break;
    }
  }
  if (fl) {
    cout << 1 << endl;
    return;
  }
  cout << 2 << endl;
}
signed main() {
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
  long long tin = 1;
  cin >> tin;
  while (tin--) {
    stand8th();
  }
  return 0;
}
