#include <bits/stdc++.h>
using namespace std;
int main() {
  ios_base::sync_with_stdio(false);
  int Q;
  cin >> Q;
  while (Q--) {
    int n;
    cin >> n;
    vector<int> A(n);
    vector<int> sma(n + 1, (int)(1e9));
    vector<int> big(n + 1, 0);
    for (int i = 0; i < n; i++) {
      cin >> A[i];
      sma[A[i]] = min(sma[A[i]], i);
      big[A[i]] = max(big[A[i]], i);
    }
    sort(A.begin(), A.end());
    A.resize(unique(A.begin(), A.end()) - A.begin());
    int ans = 0;
    for (int i = 0; i < A.size();) {
      int j = i;
      while (i + 1 < A.size() && big[A[i]] < sma[A[i + 1]]) i++;
      ans = max(ans, i - j + 1);
      i++;
    }
    cout << A.size() - ans << '\n';
  }
  return 0;
}
