#include <bits/stdc++.h>
using namespace std;
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 arr[n];
    int flag = 0, count = 0;
    for (int i = 0; i < n; i++) {
      cin >> arr[i];
      if (arr[i] != i + 1) {
        flag = 1;
      } else {
        count++;
      }
    }
    if (flag == 0) {
      cout << 0 << endl;
      continue;
    }
    long long start, end;
    for (int i = 0; i < n; i++) {
      if (arr[i] != i + 1) {
        start = i;
        break;
      }
    }
    for (int i = n - 1; i >= 0; i--) {
      if (arr[i] != i + 1) {
        end = i;
        break;
      }
    }
    if (n - (end - start + 1) == count) {
      cout << 1 << endl;
    } else {
      cout << 2 << endl;
    }
  }
  return 0;
}
