#include <bits/stdc++.h>
using namespace std;
const int N = 3e5 + 3;
const int inf = 1e9 + 3;
const int md = 1e9 + 7;
int q, n, sz;
int a[N], mx[N], mn[N], cnt[N];
int solve() {
  cin >> n;
  sz = 1;
  while (sz < n) sz += sz;
  for (int i = 1; i <= n; ++i) {
    mx[i] = -inf;
    mn[i] = inf;
    cnt[i] = 0;
  }
  for (int i = 1; i <= n; ++i) {
    cin >> a[i];
    cnt[a[i]] = 1;
    mn[a[i]] = min(mn[a[i]], i);
    mx[a[i]] = max(mx[a[i]], i);
  }
  for (int i = 1; i <= n; ++i) cnt[i] += cnt[i - 1];
  int r = 1;
  int ans = n;
  int last = 1;
  for (int i = 1; i <= n; ++i) {
    r = max(r, i);
    last = max(last, i);
    while (r + 1 <= n && mn[r + 1] > mx[last]) {
      r++;
      if (mn[r] != inf) last = r;
    }
    ans = min(ans, cnt[n] - cnt[r] + cnt[i - 1]);
  }
  return ans;
}
int main() {
  ios_base::sync_with_stdio(0);
  cin.tie(0);
  cout.tie(0);
  cin >> q;
  while (q--) {
    cout << solve() << '\n';
  }
}
