#include <bits/stdc++.h>
using namespace std;
template <typename T, typename T1>
T amax(T &a, T1 b) {
  if (b > a) a = b;
  return a;
}
template <typename T, typename T1>
T amin(T &a, T1 b) {
  if (b < a) a = b;
  return a;
}
void SOLVE() {
  long long n;
  cin >> n;
  vector<long long> v(n);
  long long cnt = 0;
  for (long long i = 0; i < n; i++) {
    cin >> v[i];
    if (v[i] == i + 1) {
      cnt++;
    }
  }
  if (cnt == n) {
    cout << 0 << "\n";
    return;
  }
  long long l = -1, r = -1;
  for (long long i = 0; i < n; i++) {
    if (v[i] != i + 1) {
      l = i;
      break;
    }
  }
  for (long long i = n - 1; i >= 0; i--) {
    if (v[i] != i + 1) {
      r = i;
      break;
    }
  }
  long long rnt = 0;
  for (long long i = l + 1; i < r; i++) {
    if (v[i] != i + 1) {
      rnt++;
    }
  }
  if (rnt == r - l - 1) {
    cout << 1 << "\n";
    return;
  } else {
    cout << 2 << "\n";
  }
}
signed main() {
  ios_base::sync_with_stdio(false);
  cin.tie(0);
  cout.tie(0);
  long long TC = 1;
  cin >> TC;
  while (TC--) {
    SOLVE();
  }
  return 0;
}
