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