#include <bits/stdc++.h>
using namespace std;
long long mod_exp(long long x, long long y, long long mm) {
  if (y == 0)
    return (1);
  else if (y % 2 == 0)
    return (mod_exp((x * x) % mm, y / 2, mm));
  else
    return ((x * mod_exp((x * x) % mm, (y - 1) / 2, mm)) % mm);
}
bool isPowerOfTwo(int n) {
  if (n == 0) return false;
  return (ceil(log2(n)) == floor(log2(n)));
}
long long solve() {
  int n;
  cin >> n;
  int arr[n];
  for (int i = 0; i < n; i++) cin >> arr[i];
  if (is_sorted(arr, arr + n)) return 0;
  int brr[n];
  for (int i = 0; i < n; i++) brr[i] = arr[i];
  sort(brr, brr + n);
  long long cnt = 0, cn = 0;
  int j = n - 1;
  while (brr[j] == arr[j]) {
    cn++;
    j--;
  }
  j = 0;
  while (brr[j] == arr[j]) {
    cn++;
    j++;
  }
  for (int i = 0; i < n; i++) {
    if (arr[i] == brr[i]) {
      cnt++;
    }
  }
  if (cnt == cn) return 1;
  return 2;
}
int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
  int t;
  cin >> t;
  while (t--) {
    cout << solve() << "\n";
  }
  return 0;
}
