#include <bits/stdc++.h>
using namespace std;
void test_case() {
  int n;
  cin >> n;
  vector<int> a(n), suf(n + 1, 0);
  vector<pair<int, int>> h(n, {300005, -1});
  for (int i = 0; i < n; i++) {
    cin >> a[i];
    a[i]--;
    h[a[i]].first = min(h[a[i]].first, i);
    h[a[i]].second = max(h[a[i]].second, i);
  }
  int la = n - 1;
  suf[n - 1] = 0;
  int pres = (h[n - 1].second >= 0);
  for (int i = n - 2; i >= 0; i--) {
    if (h[i].second >= 0) {
      if (h[i].second >= h[la].first) {
        suf[i] = pres;
      } else {
        suf[i] = suf[i + 1];
      }
      pres++;
      la = i;
    } else {
      suf[i] = suf[i + 1];
    }
  }
  int ans = suf[0], pre = 0;
  for (int i = 0; i < n; i++) {
    if (h[i].second >= 0) {
      pre++;
    }
    ans = min(ans, pre + suf[i + 1]);
  }
  cout << ans << endl;
  return;
}
int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
  int q;
  cin >> q;
  while (q--) {
    test_case();
  }
  return 0;
}
