#include <bits/stdc++.h>
using namespace std;
const int N = 3e5 + 10;
int T = 1, n, x, l[N], r[N], dp[N];
void solve() {
  cin >> n;
  vector<int> a;
  for (int i = 1; i <= n; ++i) l[i] = r[i] = dp[i] = 0;
  for (int i = 1; i <= n; ++i) {
    cin >> x;
    a.push_back(x);
    if (!l[x]) l[x] = i;
    r[x] = i;
  }
  sort(a.begin(), a.end());
  a.erase(unique(a.begin(), a.end()), a.end());
  int ans = n, len = a.size();
  for (int i = len - 1; i >= 0; --i) {
    if (i + 1 == a.size() || r[a[i]] >= l[a[i + 1]])
      dp[i] = 1;
    else
      dp[i] = dp[i + 1] + 1;
    ans = min(ans, len - dp[i]);
  }
  cout << ans << '\n';
}
int main() {
  ios_base::sync_with_stdio(false);
  cin >> T;
  while (T--) {
    solve();
  }
  return 0;
}
