#include <bits/stdc++.h>
using namespace std;
void solve() {
  int n, i, a, b;
  vector<int> v;
  cin >> n;
  b = 0;
  for (i = 0; i < n; i++) {
    cin >> a;
    v.push_back(a);
    if (a == i + 1) {
      b++;
    }
  }
  if (b != n) {
    for (i = n - 1; i >= 0; i--) {
      if (v[i] == i + 1) {
        b--;
      } else
        break;
    }
    for (i = 0; i < n; i++) {
      if (v[i] == i + 1) {
        b--;
      } else
        break;
    }
  }
  if (b == n) {
    cout << "0" << endl;
  } else if (b >= 1) {
    cout << "2" << endl;
  } else
    cout << "1" << endl;
}
int main() {
  ios_base::sync_with_stdio(0);
  cin.tie(0);
  cout.tie(0);
  int t;
  cin >> t;
  while (t--) solve();
}
