#include <bits/stdc++.h>
using namespace std;
const long long mod = 998244353;
void fastio() {
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
  cout.tie(NULL);
}
void solve() {
  long long n;
  cin >> n;
  std::vector<long long> v(n + 1);
  for (long long i = 1; i < n + 1; i++) cin >> v[i];
  int cnt = 0;
  for (long long i = 1; i < n + 1; i++) {
    if (cnt == 0 && v[i] != i) cnt++;
    if (cnt == 1 && v[i] == i) cnt++;
    if (cnt == 2 && v[i] != i) cnt++;
  }
  if (cnt == 0)
    cout << 0;
  else if (cnt == 3)
    cout << 2;
  else if (cnt == 1 || cnt == 2)
    cout << 1;
  cout << '\n';
}
int main() {
  fastio();
  int t = 1;
  cin >> t;
  while (t--) solve();
  return 0;
}
