#include <bits/stdc++.h>
using namespace std;
void solve() {
  int n;
  cin >> n;
  vector<int> a(n);
  for (int i = 0; i < n; ++i) {
    cin >> a[i];
    --a[i];
  }
  if (is_sorted(a.begin(), a.end())) {
    cout << 0 << '\n';
    return;
  }
  vector<int> correct_pos;
  int beg = 0, end = n - 1;
  while (a[beg] == beg) beg++;
  while (a[end] == end) end--;
  for (int i = beg; i <= end; ++i) {
    if (a[i] == i) {
      correct_pos.push_back(i);
    }
  }
  if (correct_pos.empty()) {
    cout << 1 << '\n';
    return;
  }
  cout << 2 << '\n';
}
int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(nullptr);
  int t;
  cin >> t;
  while (t--) {
    solve();
  }
  return 0;
}
