#include <bits/stdc++.h>
using namespace std;
signed main() {
  long long t;
  cin >> t;
  while (t--) {
    long long n;
    cin >> n;
    long long arr[n];
    for (long long i = 0; i < n; i++) {
      cin >> arr[i];
    }
    long long f = 0;
    for (long long i = 0; i < n; i++) {
      if (arr[i] != (i + 1)) {
        f = 1;
        break;
      }
    }
    if (f == 0) {
      cout << "0\n";
      continue;
    }
    f = 0;
    for (long long i = 0; i < n; i++) {
      if (arr[i] != (i + 1) && f % 2 == 0) {
        f++;
      } else if (arr[i] == (i + 1) && f % 2 == 1) {
        f++;
      }
    }
    if (f > 2) {
      cout << "2\n";
    } else
      cout << "1\n";
  }
  return 0;
}
