#include <bits/stdc++.h>
using namespace std;
template <typename Arg1>
void __f(const char* name, Arg1&& arg1) {
  std::cerr << name << " : " << arg1 << endl;
}
template <typename Arg1, typename... Args>
void __f(const char* names, Arg1&& arg1, Args&&... args) {
  const char* comma = strchr(names + 1, ',');
  std::cerr.write(names, comma - names) << " : " << arg1 << " | ";
  __f(comma + 1, args...);
}
long long int solve() {
  long long int n;
  cin >> n;
  std::vector<long long int> v;
  long long int seg = 0;
  long long int count = 0;
  for (long long int i = 0; i < n; i++) {
    long long int a;
    cin >> a;
    v.push_back(a);
    if (a != i + 1) {
      count++;
    } else {
      if (count) {
        seg++;
        count = 0;
      }
    }
  }
  if (count) {
    seg++;
  }
  if (seg == 0) {
    cout << 0 << "\n";
  } else if (seg == 1) {
    cout << 1 << "\n";
  } else {
    cout << 2 << "\n";
  }
  return 0;
}
int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(0);
  cout.tie(0);
  long long int T = 1;
  cin >> T;
  while (T--) {
    solve();
  }
  return 0;
}
