#include <bits/stdc++.h>
using namespace std;
int main() {
  int T;
  cin >> T;
  while (T--) {
    int n;
    cin >> n;
    int a[n];
    for (int i = 0; i < n; i++) {
      cin >> a[i];
    }
    int b[n];
    for (int i = 0; i < n; i++) {
      b[i] = a[i];
    }
    sort(b, b + n);
    int i = 0, j = n - 1;
    for (int k = 0; k < n; k++) {
      if (a[k] != b[k]) {
        i = k;
        break;
      }
      i++;
    }
    for (int k = n - 1; k >= 0; k--) {
      if (a[k] != b[k]) {
        j = k;
        break;
      }
      j--;
    }
    if (i > j) {
      cout << "0" << endl;
    } else {
      int p = 0;
      for (int k = i; k <= j; k++) {
        if (a[k] == b[k]) {
          p = 1;
          break;
        }
      }
      if (p == 1) {
        cout << "2" << endl;
      } else
        cout << "1" << endl;
    }
  }
  return 0;
}
