#include <bits/stdc++.h>
using namespace std;
template <class T>
bool umin(T& a, T b) {
  if (a > b) {
    a = b;
    return 1;
  }
  return 0;
}
template <class T>
bool umax(T& a, T b) {
  if (a < b) {
    a = b;
    return 1;
  }
  return 0;
}
int pos[200009];
int main() {
  int t;
  scanf("%d", &t);
  while (t--) {
    int n, cnt = 0;
    scanf("%d", &n);
    for (int i = 1; i <= n; i++) {
      int x;
      scanf("%d", &x);
      pos[x] = i;
    }
    for (int i = 1; i <= n; i++) cnt += (pos[i] == i);
    if (cnt == n)
      puts("0");
    else if (!pos)
      puts("1");
    else {
      int l = 1, r = n;
      while (pos[l] == l) l++;
      while (pos[r] == r) r--;
      if (r - l + 1 == n - cnt)
        puts("1");
      else
        puts("2");
    }
  }
  return 0;
}
