#include <bits/stdc++.h>
using namespace std;
template <typename T>
ostream &operator<<(ostream &output, const vector<T> &v) {
  if (v.empty()) return output;
  for (int i = 0; i < v.size() - 1; i++) output << v[i] << " ";
  output << v.back();
  return output;
}
template <typename T>
istream &operator>>(istream &input, vector<T> &v) {
  for (auto &i : v) cin >> i;
  return input;
}
void __sol() {
  int n;
  cin >> n;
  vector<int> num(n);
  cin >> num;
  vector<int> cpr(num.begin(), num.end());
  sort(cpr.begin(), cpr.end());
  cpr.resize(distance(cpr.begin(), unique(cpr.begin(), cpr.end())));
  for (auto &i : num) i = lower_bound(cpr.begin(), cpr.end(), i) - cpr.begin();
  int sz = cpr.size();
  vector<pair<int, int>> v(sz, {INT_MAX, INT_MIN});
  for (int i = 0; i < n; i++) {
    int x = num[i];
    v[x].first = min(i, v[x].first);
    v[x].second = max(i, v[x].second);
  }
  int cnt = 1;
  int last = v[0].second;
  int ans = sz - 1;
  for (int i = 1; i < sz; i++) {
    if (last < v[i].first) {
      cnt++;
      last = v[i].second;
    } else {
      ans = min(ans, sz - cnt);
      cnt = 1;
      last = v[i].second;
    }
  }
  ans = min(ans, sz - cnt);
  cout << ans << "\n";
  return;
}
int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(0);
  cout.tie(0);
  int tc = 1;
  cin >> tc;
  while (tc--) __sol();
  return 0;
}
