#include <bits/stdc++.h>
using namespace std;
int main() {
  long long t, n;
  cin >> t;
  while (t--) {
    cin >> n;
    int a[n];
    for (int i = 0; i < n; i++) cin >> a[i];
    bool f = false;
    for (int i = 0; i < n - 1; i++) {
      if (a[i] > a[i + 1]) {
        f = true;
        break;
      }
    }
    if (f) {
      int l = 0, h = n - 1;
      for (int i = 0; i < n; i++) {
        if (a[i] != (i + 1)) {
          l = i;
          break;
        }
      }
      for (int i = n - 1; i >= 0; i--) {
        if (a[i] != (i + 1)) {
          h = i;
          break;
        }
      }
      int k = 0;
      for (int i = l; i <= h; i++) {
        if (a[i] == (i + 1)) {
          k++;
          break;
        }
      }
      if (k)
        cout << "2";
      else
        cout << "1";
    } else {
      cout << "0";
    }
    cout << endl;
  }
  return 0;
}
