#include <bits/stdc++.h>
using namespace std;
const long long MOD = 1e+9 + 7;
const long long INF = 0x7f7f7f7f7f7f7f7f;
const int INFi = 0x7f7f7f7f;
const long long MAXN = 2e+3 + 500;
const long long MAXN1 = 2e+6 + 8;
set<long long> sa, sb;
void Divisors(long long n, set<long long>& s) {
  for (int i = 2; i <= sqrt(n); i++) {
    if (n % i == 0) {
      s.insert(i);
    }
  }
}
int main() {
  int t;
  cin >> t;
  while (t--) {
    int n;
    cin >> n;
    int a[n + 1];
    int mark[n + 1];
    for (int i = 1; i <= n; i++) {
      cin >> a[i];
      if (a[i] == i)
        mark[i] = 0;
      else
        mark[i] = 1;
    }
    int first = 1, last = n;
    while (mark[first] == 0 and first <= n) first++;
    while (mark[last] == 0 and last >= 1) last--;
    if (first == n + 1) {
      cout << 0 << endl;
      continue;
    }
    int flag = 0;
    for (int i = first; i <= last; i++)
      if (!mark[i]) {
        flag = 1;
        break;
      }
    if (flag)
      cout << 2 << endl;
    else
      cout << 1 << endl;
  }
}
