#include <bits/stdc++.h>
using namespace std;
int main() {
  ios::sync_with_stdio(false);
  cin.tie(0);
  int tt;
  cin >> tt;
  while (tt--) {
    int n;
    cin >> n;
    vector<int> a(n + 1);
    for (int i = 1; i <= n; i++) {
      cin >> a[i];
    }
    int lo = 1, hi = n;
    while (a[lo] == lo && lo <= n) lo++;
    while (a[hi] == hi && hi >= 0) hi--;
    if (lo == n + 1 || hi == -1) {
      cout << 0 << '\n';
      continue;
    }
    int ans = 1;
    for (int i = lo; i <= hi; i++) {
      if (a[i] == i) {
        ans++;
        break;
      }
    }
    cout << ans << '\n';
  }
  return 0;
}
