#include <bits/stdc++.h>
using namespace std;
const int INF = 1e9 + 9;
const int MAXN = 3e5 + 1;
int l[MAXN], r[MAXN];
signed main() {
  ios_base::sync_with_stdio(false);
  cin.tie(0);
  cout.tie(0);
  int t;
  cin >> t;
  while (t--) {
    int n;
    cin >> n;
    vector<int> v(n);
    for (int i = (int)(0); i < (int)(n); ++i) {
      l[i] = INF;
      r[i] = -INF;
    }
    for (int i = (int)(0); i < (int)(n); ++i) {
      cin >> v[i];
      --v[i];
      l[v[i]] = min(l[v[i]], i);
      r[v[i]] = max(r[v[i]], i);
    }
    sort(v.begin(), v.end());
    v.erase(unique(v.begin(), v.end()), v.end());
    vector<int> dp(n, 0);
    int ans = n;
    for (int i = 0; i < v.size(); ++i) {
      if (!i || l[v[i]] <= r[v[i - 1]])
        dp[i] = 1;
      else
        dp[i] = dp[i - 1] + 1;
      ans = min(ans, (int)(v.size() - dp[i]));
    }
    cout << ans << '\n';
  }
  return 0;
}
