#include <bits/stdc++.h>
using namespace std;
template <typename Arg1>
void __f(const char* name, Arg1&& arg1) {
  cerr << name << " : " << arg1 << std::endl;
}
template <typename Arg1, typename... Args>
void __f(const char* names, Arg1&& arg1, Args&&... args) {
  const char* comma = strchr(names + 1, ',');
  cerr.write(names, comma - names) << " : " << arg1 << " | ";
  __f(comma + 1, args...);
}
int lis(vector<int> const& a) {
  int n = a.size();
  const int INF = 1e9;
  vector<int> dp(n);
  dp[1] = 1;
  for (int i = 0; i < n; i++) {
    dp[i] = 1;
    for (int j = 0; j < i; j++) {
      if (a[j] == a[i] - 1) {
        dp[i] = 1 + dp[j];
      }
    }
  }
  int ans = 1;
  for (int i = 0; i < n; i++) {
    ans = max(ans, dp[i]);
  }
  return ans;
}
int main() {
  srand(chrono::steady_clock::now().time_since_epoch().count());
  ios_base::sync_with_stdio(0);
  cin.tie(0);
  cout.tie(0);
  int t;
  cin >> t;
  while (t--) {
    long long int n;
    cin >> n;
    vector<int> a(n);
    for (__typeof(n) i = (0) - ((0) > (n)); i != (n) - ((0) > (n));
         i += 1 - 2 * ((0) > (n))) {
      cin >> a[i];
    }
    auto b = a;
    sort(b.begin(), b.end());
    map<int, int> g;
    for (int i = 0; i < n; i++) {
      g[b[i]] = i;
    }
    for (int i = 0; i < n; i++) {
      a[i] = g[a[i]];
    }
    long long int len = lis(a);
    cout << n - len << "\n";
  }
  return 0;
}
