#include <bits/stdc++.h>
using namespace std;
const int MAXN = 5e5 + 1;
const int M = 1e9 + 7;
void solve() {
  int n;
  scanf("%d", &n);
  vector<int> v(n);
  set<int> s;
  map<int, int> fo, lo, occ;
  for (int i = 0; i < n; i++) {
    scanf("%d", &v[i]);
    if (s.find(v[i]) == s.end()) {
      s.insert(v[i]);
      fo[v[i]] = i;
    }
    lo[v[i]] = i;
  }
  int dp[n + 1];
  memset(dp, 0, sizeof(dp));
  int maxi = 0;
  for (int i = 0; i < n; i++) {
    if (i == fo[v[i]]) {
      auto it = s.find(v[i]);
      if (it == s.begin()) {
        dp[i] = 1;
      } else {
        --it;
        int val = *it;
        if (lo[val] > i) {
          dp[i] = 1;
        } else {
          dp[i] = dp[lo[val]] + 1;
        }
      }
    } else {
      int ind = occ[v[i]];
      dp[i] = dp[ind] + 1;
    }
    maxi = max(maxi, dp[i]);
    occ[v[i]] = i;
  }
  maxi = n - maxi;
  printf("%d\n", maxi);
}
int main() {
  int t;
  scanf("%d", &t);
  while (t--) {
    solve();
  }
  return 0;
}
