#include <bits/stdc++.h>
using namespace std;
#pragma GCC push_options
#pragma GCC optimize("unroll-loops")
int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(0);
  cout.tie(0);
  long long t;
  cin >> t;
  while (t--) {
    long long n;
    cin >> n;
    long long ar[n];
    long long left[n + 1];
    for (int i = 0; i <= n; i++) left[i] = 1000000007;
    long long right[n + 1];
    for (int i = 0; i <= n; i++) right[i] = -1ll;
    long long count = 0ll;
    for (long long i = 0; i < n; i++) {
      cin >> ar[i];
      if (left[ar[i]] == 1000000007) count++;
      left[ar[i]] = min(left[ar[i]], i);
      right[ar[i]] = max(right[ar[i]], i);
    }
    vector<pair<long long, long long> > ashu;
    long long ans = 1000000007;
    for (int i = 1; i <= n; i++) {
      if (right[i] != -1) {
        if (ashu.size() == 0 || ashu.back().second > left[i]) ashu.clear();
        ashu.emplace_back(make_pair(left[i], right[i]));
        ans = min(ans, count - (long long)ashu.size());
      }
    }
    cout << ans << "\n";
  }
  return 0;
}
