#include <bits/stdc++.h>
using namespace std;
void solve() {
  int n;
  cin >> n;
  vector<int> a(n);
  for (int& x : a) cin >> x;
  vector<int> dp(n, 1);
  map<int, int> pos;
  vector<int> b = a;
  sort(b.begin(), b.end());
  for (int i = 0; i < n; i++) pos[b[i]] = i;
  for (int i = n - 1; i > -1; i--) {
    for (int j = i + 1; j < n; j++) {
      if (pos[a[i]] != n - 1 && a[j] == b[pos[a[i]] + 1]) dp[i] = dp[j] + 1;
    }
  }
  int mx = 0;
  for (int& x : dp) mx = max(x, mx);
  cout << n - mx;
}
int main() {
  int t;
  cin >> t;
  while (t--) {
    solve();
    cout << endl;
  }
  return 0;
}
