#include <bits/stdc++.h>
using namespace std;
int main() {
  int t;
  cin >> t;
  while (t--) {
    int n, arr[200010], st = 0, en = 0, cnt = 0;
    cin >> n;
    for (int i = 1; i <= n; i++) cin >> arr[i];
    for (int i = 1; i <= n; i++)
      if (arr[i] != i) {
        st = i;
        break;
      }
    for (int i = n; i > 0; i--)
      if (arr[i] != i) {
        en = i;
        break;
      }
    for (int i = st + 1; i <= en - 1; i++)
      if (arr[i] != i) cnt++;
    if (!st)
      cout << 0 << endl;
    else if (cnt == en - st - 1)
      cout << 1 << endl;
    else
      cout << 2 << endl;
  }
  return 0;
}
