#include <bits/stdc++.h>
using namespace std;
int64_t a[200005];
signed main() {
  int64_t t;
  cin >> t;
  while (t--) {
    int64_t n, i;
    cin >> n;
    for (i = 1; i <= n; i++) cin >> a[i];
    int64_t c = 0, f = 0, fl = 0;
    a[0] = 1000000000;
    int64_t cou = 0;
    for (i = 1; i <= n; i++) {
      if (a[i] != i) fl = 1;
      if (a[i] == i && a[i - 1] != i - 1) cou++;
    }
    if (fl == 0)
      cout << "0"
           << "\n";
    else if (cou == 0)
      cout << "1"
           << "\n";
    else if (cou == 1 && (a[1] == 1 || a[n] == n))
      cout << cou << "\n";
    else if (cou == 2 && (a[1] == 1 && a[n] == n))
      cout << "1"
           << "\n";
    else
      cout << "2"
           << "\n";
  }
}
