#include <bits/stdc++.h>
using namespace std;
const long long mod = 1e9 + 7;
const long long inf = 0x3f3f3f3f;
const long double epsilon = 10e-9;
const long long nax = 0;
void solve() {
  long long n;
  cin >> n;
  long long arr[n + 5], ans = 0, kebetulan = 0;
  for (int i = 1; i <= n; i++) {
    cin >> arr[i];
  }
  long long l = 1, r = n;
  while (arr[l] == l && l <= n) l++;
  while (arr[r] == r && r > 0) r--;
  if (l >= r) {
    cout << 0 << '\n';
    return;
  }
  for (int i = l + 1; i < r; i++) {
    if (arr[i] == i) {
      cout << 2 << '\n';
      return;
    }
  }
  cout << 1 << '\n';
}
int main(void) {
  ios_base::sync_with_stdio(0);
  cin.tie();
  cout.tie();
  int t;
  cin >> t;
  while (t--) {
    solve();
  }
}
