#include <bits/stdc++.h>
using namespace std;
void solve() {
  int n;
  cin >> n;
  set<int> q;
  map<int, int> conflicts;
  for (int i = 0; i < n; ++i) {
    int val;
    cin >> val;
    auto it = q.upper_bound(val);
    if (it != q.end())
      conflicts[val] = *it;
    else
      conflicts[val] = INT_MAX;
    q.insert(val);
  }
  int sz = q.size();
  vector<int> elements(sz), prob(sz);
  int i = 0;
  for (auto it = conflicts.begin(); it != conflicts.end(); ++it) {
    elements[i] = it->first;
    prob[i] = it->second;
    ++i;
  }
  vector<int> smp(sz);
  smp[sz - 1] = prob[sz - 1];
  for (int i = sz - 2; i >= 0; --i) smp[i] = min(smp[i + 1], prob[i]);
  int ans = INT_MAX;
  for (int i = 0; i < sz; ++i) {
    int Y = smp[i];
    int pos =
        lower_bound(elements.begin(), elements.end(), Y) - elements.begin();
    ans = min(ans, i + sz - pos);
  }
  cout << ans << '\n';
}
int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(nullptr);
  cout.tie(nullptr);
  int t;
  cin >> t;
  while (t--) solve();
  return 0;
}
