#include <bits/stdc++.h>
using namespace std;
const int maxn = 1e6 + 5;
vector<long long> V;
int pre[maxn];
int main() {
  int T;
  cin >> T;
  while (T--) {
    int n;
    cin >> n;
    for (int i = 1; i <= n; i++) cin >> pre[i];
    bool ok = 1;
    for (int i = 1; i <= n; i++)
      if (pre[i] != i) ok = 0;
    if (ok) {
      cout << 0 << endl;
      continue;
    }
    bool now = 0;
    int cnt = 0;
    for (int i = 1; i <= n; i++) {
      if (pre[i] != i) {
        if (!now) {
          now = 1;
          cnt++;
        }
      } else
        now = 0;
    }
    if (cnt >= 2)
      cout << 2 << endl;
    else
      cout << 1 << endl;
  }
  return 0;
}
