#include <bits/stdc++.h>
using namespace std;
int main() {
  int t;
  cin >> t;
  while (t) {
    int n;
    cin >> n;
    vector<int> a(n);
    for (int i = 0; i < n; i++) {
      cin >> a[i];
    }
    int startPtr = 0, endPtr = n - 1, moves = 0;
    while (1) {
      if (a[startPtr] == startPtr + 1) {
        startPtr++;
      } else {
        while (a[endPtr] == endPtr + 1) {
          endPtr--;
        }
        break;
      }
      if (a[endPtr] == endPtr + 1) {
        endPtr--;
      } else {
        while (a[startPtr] == startPtr + 1) {
          startPtr++;
        }
        break;
      }
      if (startPtr > endPtr) {
        break;
      }
    }
    if (startPtr <= endPtr) {
      moves++;
      while (startPtr <= endPtr) {
        if (a[startPtr] == startPtr + 1 || a[endPtr] == endPtr + 1) {
          moves++;
          break;
        }
        startPtr++;
        endPtr--;
      }
    }
    cout << moves << endl;
    t--;
  }
  return 0;
}
