#include <bits/stdc++.h>
using namespace std;
bool isPrime(long long n) {
  if (n <= 1) return false;
  if (n <= 3) return true;
  if (n % 2 == 0 || n % 3 == 0) return false;
  for (int i = 5; i * i <= n; i = i + 6)
    if (n % i == 0 || n % (i + 2) == 0) return false;
  return true;
}
bool sortbysec(const pair<int, int> &a, const pair<int, int> &b) {
  return (a.second < b.second);
}
int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
  long long t;
  cin >> t;
  while (t--) {
    long long n;
    cin >> n;
    long long a[n];
    long long cnt = 0, f = 0;
    for (int i = 0; i < n; i++) {
      cin >> a[i];
      a[i]--;
    }
    for (int i = 0; i < n; i++) {
      if (a[i] == i) {
        cnt++;
        f++;
      }
    }
    long long i = 0, j = n - 1;
    while (i < n && a[i] == i) {
      f--;
      i++;
    }
    while (j > i && a[j] == j) {
      f--;
      j--;
    }
    if (cnt == n)
      cout << "0" << endl;
    else if (f > 0)
      cout << "2" << endl;
    else
      cout << "1" << endl;
  }
}
