#include #include using namespace std; #define rep(i,n) for(int i = 0; i < (int)(n); i++) using ll = long long; constexpr int inf = 1e9; // from https://drken1215.hatenablog.com/entry/2021/08/09/235400 signed main() { int T; cin >> T; while (T--) { int N; cin >> N; vector H(N,0LL); rep(i,N) cin >> H[i]; assert(N <= 8); int ans = inf; vector cop(N,0LL); vector B = H; // B を小さい順にソート sort(B.begin(), B.end()); vector res(H.size()); for (int i = 0; i < H.size(); ++i) { res[i] = lower_bound(B.begin(), B.end(), H[i]) - B.begin(); } ranges::iota(cop,0); do { int phase = 0; if (cop[0] < cop[1]) { continue; } for (int i = 0; i < N - 1; i++) { if (phase % 2 == 0) { if (cop[i] < cop[i + 1]) phase++; } if (phase % 2 == 1) { if (cop[i] > cop[i + 1]) phase++; } } if (phase == 3) { int cnt = 0; for (int i = 0; i < N; i++) { cnt += (cop[i] != res[i]); } ans = min(ans,cnt); } }while (ranges::next_permutation(cop).found); cout << ans << endl; } }