#include <bits/stdc++.h>
using namespace std;
int a[201000];
int b[201000];
int main() {
  int t;
  scanf("%d", &t);
  while (t--) {
    int n;
    scanf("%d", &n);
    for (int i = 1; i <= n; i++) scanf("%d", &a[i]), b[i] = a[i];
    sort(b + 1, b + 1 + n);
    int l = 1, r = n;
    while (l <= r && a[l] == b[l]) l++;
    while (l <= r && a[r] == b[r]) r--;
    int ok = 0;
    if (l > r)
      printf("0\n");
    else {
      for (int i = l; i <= r; i++) {
        if (a[i] == b[i]) ok = 1;
      }
      printf("%d\n", 1 + ok);
    }
  }
}
