#include <bits/stdc++.h>
const int INF = 0x3f3f3f3f;
using namespace std;
int a[3030], b[3030];
int dp[3030];
int main() {
  int t;
  cin >> t;
  while (t--) {
    int n;
    memset(dp, 0, sizeof(dp));
    cin >> n;
    for (int i = 1; i <= n; i++) {
      cin >> a[i];
      b[i] = a[i];
    }
    sort(b + 1, b + 1 + n);
    for (int i = 1; i <= n; i++) {
      a[i] = lower_bound(b + 1, b + n + 1, a[i]) - b;
    }
    int ans = 0;
    for (int i = 1; i <= n; i++) {
      dp[a[i]] = dp[a[i] - 1] + 1;
      ans = max(ans, dp[a[i]]);
    }
    cout << n - ans << endl;
  }
}
