#include <bits/stdc++.h>
using namespace std;
const int N = 2e5 + 7;
int a[N];
int main() {
  int i, j, k, n, t;
  int l, r;
  cin >> t;
  while (t--) {
    cin >> n;
    for (i = 1; i <= n; i++) cin >> a[i];
    int ans = 0;
    for (i = 1; i <= n; i++) {
      if (a[i] != i) {
        ans = 1;
        l = i;
        break;
      }
    }
    if (ans == 0)
      printf("0\n");
    else {
      for (i = n; i > l; i--) {
        if (a[i] != i) {
          r = i;
          break;
        }
      }
      for (i = l; i <= r; i++) {
        if (a[i] == i) {
          ans = 2;
          break;
        }
      }
      printf("%d\n", ans);
    }
  }
}
