#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; 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; auto cop = H; do { int phase = 0; if (H[0] < H[1]) { continue; } for (int i = 0; i < N - 1; i++) { if (phase % 2 == 0) { if (H[i] < H[i + 1]) phase++; } if (phase % 2 == 1) { if (H[i] > H[i + 1]) phase++; } } if (phase == 3) { int cnt = 0; for (int i = 0; i < N; i++) { cnt += (cop[i] != H[i]); } ans = min(ans,cnt); } }while (ranges::next_permutation(H).found); cout << ans << endl; } }