#include <bits/stdc++.h>
using namespace std;
const int LIM = 2e5 + 5;
long long a[LIM], b[LIM];
int solve() {
  int m;
  cin >> m;
  int flag = 1;
  int flag2 = 0;
  for (int i = 0; i < m; i++) {
    cin >> a[i];
    b[i] = a[i];
    if (i > 0) {
      if (a[i] < a[i - 1]) {
        flag = 0;
      }
    }
  }
  sort(a, a + m);
  int early = 1;
  for (int i = 0; i < m; i++) {
    if (a[i] != b[i]) {
      early = 0;
    }
    if (a[i] == b[i] && flag2 == 0 && early == 0) {
      flag2 = 1;
    }
    if (a[i] != b[i] && flag2 == 1) {
      flag2 = 2;
    }
  }
  if (flag) {
    cout << 0 << endl;
  } else if (flag2 == 2) {
    cout << 2 << endl;
  } else {
    cout << 1 << endl;
  }
  return 0;
}
int main() {
  int n;
  cin >> n;
  for (int i = 0; i < n; i++) {
    solve();
  }
}
