#include <bits/stdc++.h>
using namespace std;
const int MOD = 998244353;
const long long mod = 1e9 + 7;
const int mxN = 1e6 + 9;
void solve() {
  int n, tmp;
  cin >> n;
  vector<int> w;
  for (int i = 1; i <= n; i++) {
    cin >> tmp;
    if (tmp != i) w.push_back(i);
  }
  if (w.size() == 0) {
    cout << 0 << "\n";
    return;
  }
  for (int i = 1; i < (int)w.size(); i++) {
    if (w[i] - w[i - 1] > 1) {
      cout << 2 << "\n";
      return;
    }
  }
  cout << 1 << "\n";
}
int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(0);
  int t;
  cin >> t;
  while (t--) {
    solve();
  }
}
