#include <bits/stdc++.h>
using namespace std;
signed main() {
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
  cout.tie(NULL);
  long long t;
  cin >> t;
  while (t--) {
    long long n;
    cin >> n;
    long long a[n];
    vector<vector<long long>> adj(n + 1);
    set<long long> st;
    for (int i = 0; i < n; i++) {
      cin >> a[i];
      adj[a[i]].push_back(i);
      st.insert(a[i]);
    }
    long long count = st.size();
    long long ans = n;
    long long cnt = 0;
    long long last = -1;
    for (int i = 1; i <= n; i++) {
      if (adj[i].size() > 0) {
        long long start = adj[i][0];
        long long end = adj[i].back();
        if (start > last) {
          cnt++;
          last = end;
        } else {
          cnt = 1;
          last = end;
        }
        ans = min(ans, count - cnt);
      }
    }
    cout << ans << endl;
  }
}
