#include <bits/stdc++.h>
using namespace std;
void clean(vector<int> &arr) {
  map<int, int> table;
  for (int x : arr) table[x] = 0;
  int id = 0;
  for (auto [x, _] : table) table[x] = id++;
  for (int &x : arr) x = table[x];
}
int main() {
  int t;
  cin >> t;
  while (t--) {
    int n;
    cin >> n;
    vector<int> a(n);
    for (int &ai : a) cin >> ai;
    clean(a);
    vector<int> dp(n, 1);
    for (int i = 0; i < n; i++) {
      for (int j = 0; j < i; j++) {
        if (0 <= a[i] - a[j] and a[i] - a[j] <= 1) {
          dp[i] = max(dp[j] + 1, dp[i]);
        }
      }
    }
    int ans = dp[0];
    for (int x : dp) ans = max(ans, x);
    cout << n - ans << endl;
  }
}
