#include <bits/stdc++.h>
using namespace std;
const int inf = (int)1.01e9;
const long long infll = (long long)1.01e18;
const long double eps = 1e-9;
const long double pi = acos((long double)-1);
mt19937 mrand(chrono::steady_clock::now().time_since_epoch().count());
int rnd(int x) { return mrand() % x; }
void precalc() {}
const int maxn = (int)3e5 + 5;
int n;
int a[maxn];
bool read() {
  if (scanf("%d", &n) < 1) {
    return false;
  }
  for (int i = 0; i < n; i++) {
    scanf("%d", &a[i]);
  }
  return true;
}
vector<int> xs;
int ls[maxn], rs[maxn];
void solve() {
  xs.clear();
  for (int i = 0; i < n; i++) {
    xs.push_back(a[i]);
  }
  sort(xs.begin(), xs.end());
  xs.erase(unique(xs.begin(), xs.end()), xs.end());
  for (int i = 0; i < n; i++) {
    a[i] = lower_bound(xs.begin(), xs.end(), a[i]) - xs.begin();
  }
  for (int i = 0; i < ((int)(xs).size()); i++) {
    ls[i] = inf;
    rs[i] = -inf;
  }
  for (int i = 0; i < n; i++) {
    ls[a[i]] = min(ls[a[i]], i);
    rs[a[i]] = max(rs[a[i]], i);
  }
  int res = ((int)(xs).size());
  for (int j = 0; j < ((int)(xs).size()); j++) {
    int i = j;
    while (j + 1 < ((int)(xs).size()) && rs[j] < ls[j + 1]) {
      j++;
    }
    res = min(res, ((int)(xs).size()) - (j - i + 1));
  }
  printf("%d\n", res);
}
int main() {
  precalc();
  int t;
  scanf("%d", &t);
  while (read()) {
    solve();
  }
  return 0;
}
