#include <bits/stdc++.h>
using namespace std;
int main() {
  int t;
  cin >> t;
  while (t--) {
    int a, countright = 0, countwrong = 0, sltofwrong = 0, flt = 0;
    cin >> a;
    int ar[a + 1];
    ar[0] = 0;
    for (int i = 1; i <= a; i++) {
      cin >> ar[i];
    }
    for (int i = 1; i <= a; i++) {
      if (ar[i] != i && sltofwrong == 0) {
        sltofwrong = i;
      }
      if (ar[i] != i) {
        flt = i;
      }
    }
    for (int i = sltofwrong; i <= flt; i++) {
      if (i == ar[i])
        countright++;
      else if (i != ar[i])
        countwrong++;
    }
    if (countwrong == 0)
      cout << 0 << endl;
    else if (countright == 0)
      cout << 1 << endl;
    else
      cout << 2 << endl;
  }
}
