#include <bits/stdc++.h>
using namespace std;
const int maxn = 3e5 + 2, inf = 1e9;
vector<int> compress;
map<int, int> M;
int a[maxn], dp[maxn], fir[maxn], las[maxn], cnt[maxn];
int n;
int main() {
  ios::sync_with_stdio(false);
  cin.tie(0);
  int t;
  cin >> t;
  while (t--) {
    cin >> n;
    for (int i = 0; i < n; i++) {
      cin >> a[i];
      compress.push_back(a[i]);
    }
    sort(compress.begin(), compress.end());
    compress.resize(unique(compress.begin(), compress.end()) -
                    compress.begin());
    for (int i = 0; i < compress.size(); i++) {
      M[compress[i]] = i;
    }
    int k = compress.size();
    for (int i = 0; i < n; i++) {
      a[i] = M[a[i]];
      las[a[i]] = i;
      cnt[a[i]]++;
    }
    for (int i = n - 1; i >= 0; i--) {
      fir[a[i]] = i;
    }
    for (int i = k - 1; i >= 0; i--) {
      dp[i] = i;
      if (i != k - 1 && fir[i + 1] > las[i]) dp[i] = dp[i + 1];
    }
    int ans = inf;
    for (int i = 0; i < k; i++) {
      ans = min(ans, i + k - dp[i] - 1);
    }
    cout << ans << '\n';
    for (int i = 0; i < n; i++) {
      cnt[i] = 0;
      fir[i] = 0;
      las[i] = 0;
    }
    compress.clear();
    M.clear();
  }
}
