#include <bits/stdc++.h>
using namespace std;
mt19937 rng(chrono::steady_clock::now().time_since_epoch().count());
void solve() {
  long long n;
  cin >> n;
  vector<long long> a(n), b;
  for (long long i = 0; i < n; i++) {
    cin >> a[i];
  }
  b = a;
  sort(b.begin(), b.end());
  b.resize(unique(b.begin(), b.end()) - b.begin());
  for (long long i = 0; i < n; i++) {
    a[i] = lower_bound(b.begin(), b.end(), a[i]) - b.begin();
  }
  vector<long long> dp(n, -1e9);
  dp[a[0]] = 1;
  for (long long i = 1; i < n; i++) {
    dp[a[i]] = 1;
    if (a[i]) dp[a[i]] = max(dp[a[i]], dp[a[i] - 1] + 1);
  }
  long long ans = n - *max_element(dp.begin(), dp.end());
  cout << ans << '\n';
}
signed main() {
  ios_base::sync_with_stdio(0), cin.tie(0), cout.tie(0);
  long long q;
  cin >> q;
  while (q--) {
    solve();
  }
}
