#include <bits/stdc++.h>
using namespace std;
bool us[300005];
vector<int> tmp;
long long q, n, v[300005], pos[300005][2], l, r, ans;
int main() {
  ios::sync_with_stdio(false);
  cin.tie(0);
  cin >> q;
  while (q--) {
    cin >> n;
    tmp.clear();
    for (int i = 0; i <= n; i++) us[i] = false;
    for (int i = 0; i < n; i++) {
      cin >> v[i];
      if (us[v[i]] == false) tmp.push_back(v[i]);
      us[v[i]] = true;
    }
    sort(tmp.begin(), tmp.end());
    l = r = 0;
    ans = tmp.size() + 1;
    for (int i = 0; i < n; i++) {
      pos[v[i]][1] = i;
      pos[v[n - 1 - i]][0] = n - i - 1;
    }
    while (l < tmp.size()) {
      r = l + 1;
      while (r < tmp.size() && pos[tmp[r]][0] > pos[tmp[r - 1]][1]) r++;
      ans = min(ans, (long long)tmp.size() - (r - l));
      l = r;
    }
    cout << ans << "\n";
  }
  return 0;
}
