#include <bits/stdc++.h>
using namespace std;
const double pi = acos(-1.0);
const double eps = 1e-10;
const long long mod = 1e9 + 7;
const int INF = 0x3f3f3f3f;
const int maxn = 3e5 + 5;
long long read() {
  long long x = 0, f = 1;
  char ch = getchar();
  while (ch < '0' || ch > '9') {
    if (ch == '-') f = -1;
    ch = getchar();
  }
  while (ch >= '0' && ch <= '9') {
    x = x * 10 + ch - '0';
    ch = getchar();
  }
  return x * f;
}
void print(int x) {
  if (x < 0) putchar('-'), x = -x;
  if (x > 9) print(x / 10);
  putchar(x % 10 - '0');
}
int n;
int a[maxn];
map<int, pair<int, int> > ma;
vector<pair<int, int> > v;
int main(void) {
  int T;
  scanf("%d", &T);
  while (T--) {
    ma.clear();
    scanf("%d", &n);
    for (int i = 1; i <= n; i++) scanf("%d", &a[i]);
    for (int i = 1; i <= n; i++) {
      if (ma.find(a[i]) == ma.end()) {
        ma[a[i]].first = i;
        ma[a[i]].second = i;
      } else {
        ma[a[i]].first = min(ma[a[i]].first, i);
        ma[a[i]].second = max(ma[a[i]].second, i);
      }
    }
    v.clear();
    for (map<int, pair<int, int> >::iterator it = ma.begin(); it != ma.end();
         it++) {
      v.push_back(pair<int, int>((it->second).first, (it->second).second));
    }
    int ans = 1;
    for (int i = 0, j; i < v.size() - 1; i = j + 1) {
      int cnt = 1;
      for (j = i; j < v.size() - 1; j++) {
        if (v[j].second > v[j + 1].first) break;
        cnt++;
      }
      ans = max(ans, cnt);
    }
    printf("%d\n", ma.size() - ans);
  }
  return 0;
}
