#include <bits/stdc++.h>
using namespace std;
void solve() {
  long long int n;
  cin >> n;
  long long int a[n + 1];
  vector<long long int> v;
  v.push_back(0);
  for (long long int i = 1; i <= n; i++) {
    cin >> a[i];
    if (a[i] == i) {
      v.push_back(i);
    }
  }
  v.push_back(n + 1);
  if (v.size() == n + 2) {
    cout << 0 << "\n";
    return;
  }
  long long int po = 0;
  for (long long int i = 1; i < v.size(); i++) {
    if (v[i] - v[i - 1] > 1) {
      po++;
    }
  }
  if (po > 2) {
    po = 2;
  }
  cout << po << "\n";
}
int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
  long long int t;
  cin >> t;
  while (t--) {
    solve();
  }
  return 0;
}
