#include <bits/stdc++.h>
using namespace std;
int main() {
  ios::sync_with_stdio(false);
  cin.tie(0);
  int tt;
  cin >> tt;
  while (tt--) {
    int n;
    cin >> n;
    vector<int> a(n);
    map<int, int> maxInd, minInd;
    for (int i = 0; i < n; i++) cin >> a[i];
    for (int i = 0; i < n; i++) {
      maxInd[a[i]] = max(maxInd[a[i]], i);
    }
    for (int i = n - 1; i >= 0; i--) {
      if (!minInd.count(a[i]))
        minInd[a[i]] = i;
      else
        minInd[a[i]] = min(minInd[a[i]], i);
    }
    sort(a.begin(), a.end());
    a.resize(unique(a.begin(), a.end()) - a.begin());
    n = a.size();
    vector<int> dp(n);
    dp[0] = 1;
    int ans = 1;
    for (int i = 1; i < n; i++) {
      if (minInd[a[i]] > maxInd[a[i - 1]])
        dp[i] = dp[i - 1] + 1;
      else
        dp[i] = 1;
      ans = max(ans, dp[i]);
    }
    cout << n - ans << endl;
  }
  return 0;
}
