#include <bits/stdc++.h>
using namespace std;
template <class T>
long long size(const T& x) {
  return x.size();
}
template <class T>
T smod(T a, T b) {
  return (a % b + b) % b;
}
void solve() {
  long long n;
  cin >> n;
  vector<long long> A(n);
  for (long long i = (0); i < (n); ++i) cin >> A[i];
  bool found = true;
  for (long long i = (0); i < (n); ++i) found &= A[i] == i + 1;
  if (found) {
    cout << 0 << endl;
    return;
  }
  int pref = 0;
  for (long long i = (0); i < (n); ++i) {
    if (A[i] == i + 1)
      pref = i;
    else
      break;
  }
  int suff = n - 1;
  for (int i = n - 1; i >= 0; i--) {
    if (A[i] == i + 1)
      suff = i;
    else
      break;
  }
  bool foundany = false;
  for (long long i = (pref + 1); i < (suff); ++i) {
    if (A[i] == i + 1) foundany = true;
  }
  if (foundany)
    cout << 2 << endl;
  else
    cout << 1 << endl;
}
int main() {
  cin.sync_with_stdio(false);
  cin.tie(nullptr);
  cout.tie(nullptr);
  long long t;
  cin >> t;
  while (t--) solve();
  return 0;
}
