#include <bits/stdc++.h>
using namespace std;
const long long N = 2e5 + 5;
long long a[N];
long long n;
bool check1() {
  for (long long i = 1; i <= n; i++) {
    if (a[i] != i) return false;
  }
  return true;
}
bool check2() {
  long long cnt = 0;
  for (long long i = 1; i <= n; i++) {
    if (a[i] != i) {
      cnt++;
      long long j = i + 1;
      while (j <= n && a[j] != j++)
        ;
      i = j - 1;
    }
  }
  return cnt == 1;
}
void solve() {
  cin >> n;
  for (long long i = 1; i <= n; i++) cin >> a[i];
  if (check1())
    cout << 0 << '\n';
  else if (check2())
    cout << 1 << '\n';
  else
    cout << 2 << '\n';
  return;
}
signed main() {
  ios_base::sync_with_stdio(false);
  cin.tie(0);
  cout.tie(0);
  long long t;
  cin >> t;
  while (t--) {
    solve();
  }
  return 0;
}
