#include <bits/stdc++.h>
const int MAXN = 3e5 + 5;
int n, a[MAXN], ps[MAXN], sp[MAXN];
int pre[MAXN], suf[MAXN];
int pe[MAXN], sf[MAXN];
inline void Solve() {
  scanf("%d", &n);
  for (int i = 1; i <= n; ++i) scanf("%d", a + i), ps[a[i]] = 192;
  int ans = 1e9, cnt = 0;
  for (int i = 1; i <= n; ++i)
    if (ps[i] == 192) sp[i] = ++cnt;
  for (int i = 1; i <= n; ++i) {
    a[i] = sp[a[i]];
    if (!pre[a[i]]) pre[a[i]] = i;
    suf[a[i]] = i;
  }
  pe[1] = 1;
  for (int i = 2; i <= cnt; ++i)
    pe[i] = (pre[i] > suf[i - 1]) ? pe[i - 1] + 1 : 1;
  sf[cnt] = 1;
  for (int i = cnt - 1; i >= 1; --i)
    sf[i] = (suf[i] < pre[i + 1]) ? sf[i + 1] + 1 : 1;
  sf[cnt + 1] = 0;
  for (int i = 0; i <= cnt; ++i)
    ans = std::min(ans, suf[i] < pre[i + 1]
                            ? cnt - pe[i] - sf[i + 1]
                            : std::min(cnt - pe[i], cnt - sf[i + 1]));
  printf("%d\n", ans);
  for (int i = 1; i <= n; ++i)
    ps[i] = sp[i] = pre[i] = suf[i] = pe[i] = sf[i] = 0;
}
int main() {
  int T;
  scanf("%d", &T);
  while (T--) Solve();
  return 0;
}
