#include <bits/stdc++.h>
using namespace std;
using ll = long long;
using db = double;
int mod = 1000000007;
db pie = 3.1415926535;
ll help(ll val, ll a[], ll l, ll r) {
  ll ans = -1;
  ll mx = mod;
  while (l <= r) {
    ll mid = (l + r) / 2;
    if (llabs(a[mid] - val) < mx) {
      ans = mid;
      mx = llabs(a[mid] - val);
      if (a[mid] < val)
        l = mid + 1;
      else
        r = mid - 1;
    } else {
      if (a[mid] < val)
        l = mid + 1;
      else
        r = mid - 1;
    }
  }
  return ans;
}
ll sq(ll val) { return (val * val); }
int main() {
  ios_base::sync_with_stdio(0);
  cin.tie(0);
  cout.tie(0);
  int t;
  cin >> t;
  while (t--) {
    int n;
    cin >> n;
    int a[n + 1];
    int i;
    for (i = 1; i <= n; i++) {
      cin >> a[i];
    }
    i = 1;
    while (i < n && a[i] == i) {
      i++;
    }
    int l = i;
    i = n;
    while (i >= 1 && a[i] == i) {
      i--;
    }
    int r = i;
    i = l;
    int cnt = 0;
    while (i <= r) {
      if (a[i] == i) {
        cnt++;
      }
      i++;
    }
    int ans = 0;
    if (l < r) ans++;
    ans = ans + ((cnt > 0) ? 1 : 0);
    cout << ans << "\n";
  }
}
