#include <bits/stdc++.h>
using namespace std;
const int N = 1e5 + 7;
void solve() {
  long long int n;
  cin >> n;
  vector<long long int> a(n), b;
  for (auto i = 0; i < (n); i++) {
    cin >> a[i];
  };
  int cnt = 0, pos1 = 0, pos2 = 0;
  for (auto i = 0; i < (n); i++)
    if (a[i] != i + 1) {
      b.push_back((i));
      cnt++;
    }
  bool flag = true;
  long long int x = b.size();
  for (auto i = 0; i < (x - 1); i++) {
    if (b[i + 1] - b[i] != 1) flag = false;
  }
  if (cnt == 0)
    cout << 0;
  else if (flag)
    cout << 1;
  else if (cnt == 2) {
    cout << 2;
  } else if (cnt == n)
    cout << 1;
  else if (cnt > 2)
    cout << 2;
}
void template1() {
  long long int t;
  cin >> t;
  while (t--) {
    solve();
    cout << "\n";
  }
}
int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
  template1();
  return 0;
}
