#include <bits/stdc++.h>
using namespace std;
const int N = 3e5 + 5;
int l[N], r[N];
bool cmp(int x, int y) { return r[x] < l[y]; }
void solve() {
  int n;
  scanf("%d", &n);
  for (int x = 0; x < n; ++x) {
    l[x] = n;
    r[x] = -1;
  }
  for (int i = 0; i < n; ++i) {
    int x;
    scanf("%d", &x);
    --x;
    if (l[x] == n) l[x] = i;
    r[x] = i;
  }
  vector<int> used;
  for (int x = 0; x < n; ++x) {
    if (l[x] != n) used.push_back(x);
  }
  n = (int)used.size();
  int ans = n;
  int len = 1;
  for (int i = 1; i < n; ++i) {
    if (cmp(used[i - 1], used[i]))
      ++len;
    else {
      ans = min(ans, n - len);
      len = 1;
    }
  }
  ans = min(ans, n - len);
  printf("%d\n", ans);
}
int main() {
  int q;
  scanf("%d", &q);
  while (q--) solve();
}
