#include <bits/stdc++.h>
using namespace std;
const int N = 4e5;
int fen[N];
vector<int> to_clear;
void update(int i, int val) {
  for (; i < N; i |= i + 1) {
    to_clear.push_back(i);
    fen[i] = max(fen[i], val);
  }
}
int query(int i) {
  int mx = 0;
  for (; i >= 0; i = (i & (i + 1)) - 1) {
    mx = max(mx, fen[i]);
  }
  return mx;
}
void clear() {
  while (to_clear.size()) {
    fen[to_clear.back()] = 0;
    to_clear.pop_back();
  }
}
int solve() {
  int n;
  cin >> n;
  vector<int> a(n);
  for (int i = 0; i < n; ++i) {
    cin >> a[i];
  }
  vector<int> las(n + 1, 0), fir(n + 1, N);
  for (int i = 0; i < n; ++i) {
    las[a[i]] = max(las[a[i]], i);
    fir[a[i]] = min(fir[a[i]], i);
  }
  vector<int> dp(n + 1, 0);
  int max_least = 0, different = 0;
  clear();
  int prv = 0;
  for (int i = 1; i <= n; ++i) {
    if (fir[i] > las[i]) {
      continue;
    }
    ++different;
    dp[i] = 1;
    if (las[prv] < fir[i]) {
      dp[i] = dp[prv] + 1;
    }
    prv = i;
    max_least = max(max_least, dp[i]);
  }
  return different - max_least;
}
int main() {
  ios::sync_with_stdio(0);
  cin.tie(0);
  int q;
  cin >> q;
  while (q--) {
    cout << solve() << "\n";
  }
  return 0;
}
