#include <bits/stdc++.h>
using namespace std;
int main() {
  int t;
  cin >> t;
  while (t--) {
    int n;
    cin >> n;
    int a[n];
    for (int i = 0; i < n; i++) cin >> a[i];
    int temp[n];
    for (int i = 0; i < n; i++) temp[i] = 0;
    for (int i = 0; i < n; i++) {
      for (int j = 0; j < n; j++) {
        if (a[i] > a[j]) temp[i]++;
      }
    }
    int dp[n + 1];
    for (int i = 0; i <= n; i++) dp[i] = 1;
    for (int i = 1; i <= n; i++) {
      for (int j = 0; j < i; j++) {
        if ((temp[i] == temp[j] || temp[i] - 1 == temp[j]) &
            (dp[i] < dp[j] + 1))
          dp[i] = dp[j] + 1;
      }
    }
    int k = 0;
    for (int i = 0; i <= n; i++) {
      k = max(k, dp[i]);
    }
    cout << n - k << endl;
  }
}
