#include <bits/stdc++.h>
void C(void) {
  int t, n, mas[200005], res, st, en, f;
  scanf("%i", &t);
  for (int i = 0; i < t; i++) {
    res = 0, st = 0, en = 0, f = 0;
    scanf("%i", &n);
    for (int j = 0; j < n; j++) {
      scanf("%i", &mas[j]);
      if (mas[j] == j + 1) {
        res++;
        if (f == 0) st++;
      } else
        f = 1;
    }
    for (int j = n - 1; mas[j] == j + 1 && st != n; j--) en++;
    if (st == n)
      printf("0\n");
    else if (res - st - en == 0)
      printf("1\n");
    else
      printf("2\n");
  }
}
int main() {
  C();
  return 0;
}
