#include <bits/stdc++.h>
using namespace std;
int main() {
  std::ios_base::sync_with_stdio(false);
  cin.tie(0);
  cout.tie(0);
  long long int tt;
  cin >> tt;
  while (tt--) {
    long long int n;
    cin >> n;
    long long int a[n + 1];
    for (long long int(i) = 1; (i) < (n + 1); (i)++) cin >> a[i];
    long long int ind1 = -1, ind2 = -1;
    for (long long int(i) = 1; (i) < (n + 1); (i)++) {
      if (a[i] != i) {
        ind1 = i;
        break;
      }
    }
    for (long long int i = n; i >= 1; i--) {
      if (a[i] != i) {
        ind2 = i;
        break;
      }
    }
    long long int ans = 0;
    if (ind1 == -1)
      cout << 0 << "\n";
    else {
      long long int chk = 0;
      for (long long int(i) = (ind1); (i) < (ind2 + 1); (i)++) {
        if (a[i] == i) {
          chk = 1;
        }
      }
      if (chk)
        cout << 2 << "\n";
      else
        cout << 1 << "\n";
    }
  }
  return 0;
}
