#include <bits/stdc++.h>
using namespace std;
const long long MOD = 1e9 + 7;
const long long INF = 1LL << 60;
const long long N = 2e5 + 5;
long long a[N];
void solve() {
  long long n;
  cin >> n;
  vector<long long> vect;
  for (long long i = 1; i < n + 1; i++) {
    cin >> a[i];
    if (a[i] != i) {
      vect.push_back(i);
    }
  }
  if ((long long)vect.size() == 0) {
    cout << 0;
    return;
  }
  bool fl = true;
  for (long long i = 1; i < (long long)vect.size(); i++) {
    if (vect[i] - vect[i - 1] > 1) fl = false;
  }
  if (fl)
    cout << 1;
  else
    cout << 2;
}
signed main() {
  std::ios::sync_with_stdio(false);
  cin.tie(NULL);
  long long t = 1;
  cin >> t;
  while (t--) {
    solve();
    cout << "\n";
  }
  return 0;
}
