#include <bits/stdc++.h>
using namespace std;
const int maxn = 3e5 + 50;
const int mod = 1e9 + 7;
long long qp(long long a, long long n) {
  long long res = 1;
  while (n > 0) {
    if (n & 1) res = res * a % mod;
    a = a * a % mod;
    n >>= 1;
  }
  return res;
}
template <class T>
inline bool scan(T& ret) {
  char c;
  int sgn;
  if (c = getchar(), c == EOF) return 0;
  while (c != '-' && (c < '0' || c > '9')) c = getchar();
  sgn = (c == '-') ? -1 : 1;
  ret = (c == '-') ? 0 : (c - '0');
  while (c = getchar(), c >= '0' && c <= '9') ret = ret * 10 + (c - '0');
  ret *= sgn;
  return 1;
}
int n;
int a[maxn];
vector<int> xpos;
int pos[maxn][2];
int main(int argc, char* argv[]) {
  int T;
  scanf("%d", &T);
  for (int kase = 1; kase <= T; ++kase) {
    scanf("%d", &n);
    xpos.resize(n);
    for (int i = 1; i <= n; ++i) {
      scanf("%d", &a[i]);
      xpos[i - 1] = a[i];
      pos[i][0] = 3e5 + 50;
      pos[i][1] = 0;
    }
    sort(xpos.begin(), xpos.end());
    xpos.erase(unique(xpos.begin(), xpos.end()), xpos.end());
    for (int i = 1; i <= n; ++i) {
      a[i] = lower_bound(xpos.begin(), xpos.end(), a[i]) - xpos.begin() + 1;
      pos[a[i]][0] = min(pos[a[i]][0], i);
      pos[a[i]][1] = max(pos[a[i]][1], i);
    }
    int tot = xpos.size();
    int res = 1;
    int posr = pos[1][1];
    int tmp = 1;
    for (int i = 2; i <= tot; ++i) {
      if (pos[i][0] > posr) {
        posr = pos[i][1];
        tmp++;
      } else {
        posr = pos[i][1];
        res = max(res, tmp);
        tmp = 1;
      }
    }
    res = max(res, tmp);
    printf("%d\n", tot - res);
  }
  return 0;
}
