#include <bits/stdc++.h>
using namespace std;
const long long inf = 1e18;
const long long maxu = 200005;
signed main() {
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
  cout.tie(NULL);
  long long i, j, k = 0, n, t = 1, m, l = 0;
  cin >> t;
  while (t--) {
    cin >> n;
    long long a[n];
    long long b[n + 2];
    long long cnt = 0;
    long long idx = -1;
    for (i = 0; i < n; i++) {
      cin >> a[i];
      if (a[i] != i + 1) {
        if (idx == -1) {
          idx = i;
        }
        b[i] = 0;
        cnt++;
      } else
        b[i] = 1;
    }
    if (cnt == 0) {
      cout << 0 << "\n";
      continue;
    }
    if (cnt == n) {
      cout << 1 << "\n";
      continue;
    }
    long long ans = 0;
    for (i = idx; i <= idx + cnt - 1; i++) {
      if (b[i] == 0) ans++;
    }
    if (ans == cnt) {
      cout << 1 << "\n";
    } else
      cout << 2 << "\n";
  }
  return 0;
}
