#include <bits/stdc++.h>
using namespace std;
int mpow(int base, int exp);
void ipgraph(int m);
void dfs(int u, int par);
const int mod = 1000000007;
const int N = 3e5, M = 1e9 + 7;
void solve() {
  int n, i, j;
  cin >> n;
  int arr[n];
  for (i = 0; i < n; i++) cin >> arr[i];
  bool found2 = true;
  vector<int> matching;
  for (i = 0; i < n; i++) {
    if (arr[i] == i + 1) matching.push_back(i);
    if (arr[i] != i + 1) found2 = false;
  }
  if (found2) {
    cout << 0 << endl;
    return;
  } else if (matching.size() == 0) {
    cout << 1 << endl;
    return;
  }
  if (matching[0] != 0 && matching[matching.size() - 1] != n - 1) {
    cout << 2 << endl;
    return;
  }
  int allow = 0;
  for (i = 1; i < matching.size(); i++) {
    if (matching[i] != matching[i - 1] + 1) {
      if (allow == 1) {
        cout << 2 << endl;
        return;
      } else
        allow++;
    }
  }
  if (allow == 1) {
    if (matching[matching.size() - 1] != n - 1 || matching[0] != 0) {
      cout << 2 << endl;
      return;
    }
  }
  cout << 1 << endl;
}
int main() {
  int t;
  cin >> t;
  while (t--) {
    solve();
  }
  return 0;
}
