#include <bits/stdc++.h>
using namespace std;
int a[200005], d[200005];
bool check_edge_zeros(int d[], int n) {
  int i = 1;
  while (d[i] == 0) i++;
  int start = i;
  i = n;
  while (d[i] == 0) i--;
  int end = i;
  if (start > 1 || end < n) {
    for (int i = start; i <= end; i++) {
      if (d[i] == 0) return false;
    }
    return true;
  } else {
    return false;
  }
}
void solve() {
  int n;
  cin >> n;
  bool flag1 = true, flag2 = true;
  for (int i = 1; i <= n; i++) {
    cin >> a[i];
    d[i] = a[i] - i;
    if (d[i] != 0)
      flag1 = false;
    else if (d[i] == 0)
      flag2 = false;
  }
  if (flag1) {
    cout << 0 << endl;
    return;
  }
  if (flag2) {
    cout << 1 << endl;
    return;
  }
  if (check_edge_zeros(d, n)) {
    cout << 1 << endl;
    return;
  }
  cout << 2 << endl;
  return;
}
int main() {
  ios::sync_with_stdio(0);
  cin.tie(0);
  cout.tie(0);
  cout << fixed;
  cout << setprecision(15);
  ;
  int test;
  cin >> test;
  for (int i = 1; i <= test; i++) {
    solve();
  }
}
