#include <bits/stdc++.h>
using namespace std;
bool isPower(long long x) { return x && (!(x & (x - 1))); }
long long fac(long long n) {
  long long ans = 1;
  for (int i = n; i >= 1; i--) {
    ans *= n;
    n--;
  }
  return ans;
}
long long ncr(long long n, long long r) {
  long long ans = fac(n) / (fac(n - r) * fac(r));
  return ans;
}
void solve() {
  int n;
  cin >> n;
  int a[n];
  int ans = 0;
  for (int i = 0; i < n; i++) cin >> a[i];
  for (int i = 0; i < n; i++) {
    if (a[i] == i + 1) continue;
    if (i == 0 || a[i - 1] == i) ans++;
  }
  cout << min(2, ans) << endl;
}
int main() {
  long long t;
  t = 1;
  cin >> t;
  while (t--) {
    solve();
  }
}
