#include <bits/stdc++.h>
using namespace std;
int f(int a[], int n) {
  int flag = 0;
  for (int i = 0; i < n - 1; i++)
    if (a[i + 1] < a[i]) {
      flag = 1;
      break;
    }
  if (flag == 0) return 0;
  flag = 0;
  int x = -1, y = n;
  for (int i = 0; i < n; i++)
    if (a[i] == i + 1)
      x = i;
    else
      break;
  for (int i = n - 1; i >= 0; i--)
    if (a[i] == i + 1)
      y = i;
    else
      break;
  if (x >= 0 && y < n) {
    for (int i = x + 1; i < y; i++)
      if (a[i] == i + 1) {
        flag = 1;
        break;
      }
  } else if (x == -1 && y == n) {
    for (int i = 0; i < n; i++)
      if (a[i] == i + 1) {
        flag = 1;
        break;
      }
  } else if (x == -1) {
    for (int i = 0; i < y; i++)
      if (a[i] == i + 1) {
        flag = 1;
        break;
      }
  } else {
    for (int i = x + 1; i < n; i++)
      if (a[i] == i + 1) {
        flag = 1;
        break;
      }
  }
  if (flag) return 2;
  return 1;
}
signed main() {
  ios_base::sync_with_stdio(0);
  cin.tie(NULL);
  int T;
  cin >> T;
  while (T--) {
    int n;
    cin >> n;
    int a[n];
    for (int i = 0; i < n; i++) cin >> a[i];
    cout << f(a, n) << "\n";
  }
  return 0;
}
