#include <bits/stdc++.h>
using namespace std;
const int mod = 998244353;
const int N = 1000010;
int T;
int n, a[N], l[N], r[N], ans;
template <typename T>
void gi(T &x) {
  x = 0;
  register char c = getchar(), pre = 0;
  for (; c < '0' || c > '9'; pre = c, c = getchar())
    ;
  for (; c >= '0' && c <= '9'; c = getchar()) x = x * 10ll + (c ^ 48);
  if (pre == '-') x = -x;
}
void solve() {
  gi(n);
  for (int i = (1); i <= (n); i++) gi(a[i]), l[i] = n + 1, r[i] = 0;
  for (int i = (1); i <= (n); i++)
    l[a[i]] = min(l[a[i]], i), r[a[i]] = max(r[a[i]], i);
  sort(a + 1, a + n + 1), n = unique(a + 1, a + n + 1) - a - 1;
  int i = 1, j;
  ans = 0;
  for (; i <= n;) {
    j = i;
    for (; j < n && l[a[j + 1]] > r[a[j]]; ++j)
      ;
    ans = max(ans, j - i + 1), i = j + 1;
  }
  printf("%d\n", n - ans);
}
int main() {
  gi(T);
  for (; T; --T) solve();
  return 0;
}
