#include <bits/stdc++.h>
using namespace std;
void FastIO() {
  ios::sync_with_stdio(0);
  cin.tie(0);
  cout.tie(0);
}
const int N = 1e4;
int main() {
  FastIO();
  int tc, ca = 0;
  cin >> tc;
  while (tc--) {
    int n;
    cin >> n;
    vector<int> v(n);
    for (int i = 0; i < n; i++) {
      cin >> v[i];
      v[i]--;
    }
    int L = 0;
    int R = n - 1;
    while (L < n && v[L] == L) L++;
    while (R >= 0 && v[R] == R) R--;
    int mil = 0;
    int omil = 0;
    for (int i = L; i <= R; i++) {
      if (v[i] == i)
        mil++;
      else
        omil++;
    }
    int ans = 0;
    if (omil) {
      ans++;
      if (mil) ans++;
    }
    cout << ans << '\n';
  }
  return 0;
}
