#include <bits/stdc++.h>
using namespace std;
template <typename Arg1>
void pn(Arg1&& arg1) {
  cout << arg1 << "\n";
}
template <typename Arg1, typename... Args>
void pn(Arg1&& arg1, Args&&... args) {
  cout << arg1 << "\n";
  pn(args...);
}
template <typename Arg1>
void ps(Arg1&& arg1) {
  cout << arg1 << " ";
}
template <typename Arg1, typename... Args>
void ps(Arg1&& arg1, Args&&... args) {
  cout << arg1 << " ";
  ps(args...);
}
template <typename Arg1>
void pvs(Arg1&& arg1, long long n) {
  for (long long i = 0; i < n; i++) ps(arg1[i]);
}
template <typename Arg1>
void pvn(Arg1&& arg1, long long n) {
  for (long long i = 0; i < n; i++) pn(arg1[i]);
}
const unsigned int M = 1000000007;
void solve() {
  long long n, i, cnt = 0;
  cin >> n;
  vector<long long> v;
  for (i = 0; i < n; i++) {
    long long x;
    cin >> x;
    if (x != i + 1) {
      v.push_back(i + 1);
      cnt++;
    }
  }
  if (cnt == n) {
    pn("1");
    return;
  }
  if (cnt == 0) {
    pn("0");
    return;
  }
  bool flag = true;
  for (int i = 1; i < v.size(); i++) {
    if (v[i] != v[i - 1] + 1) {
      flag = false;
    }
  }
  if (flag) {
    pn("1");
  } else
    pn("2");
}
int main() {
  ios::sync_with_stdio(false);
  cin.tie(NULL);
  cout.tie(NULL);
  ;
  long long t;
  cin >> t;
  while (t--) {
    solve();
  }
}
