#include <bits/stdc++.h>
using namespace std;
const int maxn = 300030, mod = 998244353;
inline int read() {
  int x = 0, f = 0;
  char ch = getchar();
  while (ch < '0' || ch > '9') f |= ch == '-', ch = getchar();
  while (ch >= '0' && ch <= '9') x = x * 10 + ch - '0', ch = getchar();
  return f ? -x : x;
}
int T, n, a[maxn], b[maxn], bl, lft[maxn], rig[maxn];
int main() {
  T = read();
  memset(lft, 0x3f, sizeof(lft));
  while (T--) {
    n = read();
    for (int i = (1); i <= (n); i++) b[i] = a[i] = read();
    sort(b + 1, b + n + 1);
    bl = unique(b + 1, b + n + 1) - b - 1;
    for (int i = (1); i <= (n); i++)
      a[i] = lower_bound(b + 1, b + bl + 1, a[i]) - b;
    for (int i = (1); i <= (n); i++)
      lft[a[i]] = min(lft[a[i]], i), rig[a[i]] = max(rig[a[i]], i);
    int ans = 0;
    for (int i = (1); i <= (bl); i++) {
      int j = i;
      while (j < bl && rig[j] < lft[j + 1]) j++;
      ans = max(ans, j - i + 1);
      i = j;
    }
    printf("%d\n", bl - ans);
    for (int i = (1); i <= (bl); i++) lft[i] = 1e9, rig[i] = 0;
  }
}
