#include <bits/stdc++.h>
using namespace std;
template <typename T, typename V>
ostream &operator<<(ostream &os, const pair<T, V> pai) {
  return os << '(' << pai.first << ' ' << pai.second << ')';
}
template <typename T>
ostream &operator<<(ostream &os, const vector<T> v) {
  cout << '[';
  for (auto p : v) cout << p << ",";
  cout << "]";
  return os;
}
template <typename T, typename V>
ostream &operator<<(ostream &os, const set<T, V> v) {
  cout << "{";
  for (auto p : v) cout << p << ",";
  cout << "}";
  return os;
}
template <typename T, typename V>
ostream &operator<<(ostream &os, const map<T, V> v) {
  cout << "{";
  for (auto p : v) cout << p << ",";
  cout << "}";
  return os;
}
template <typename T>
void get_max(T &a, T b) {
  a = max(a, b);
}
template <typename T>
void get_min(T &a, T b) {
  a = min(a, b);
}
int N;
int A[300000];
int l[300000], r[300000];
bool x[300000];
vector<int> X;
void solve() {
  X.clear();
  cin >> N;
  for (int i = 0, _i = (N); i < _i; i++) cin >> A[i], X.push_back(A[i]);
  sort((X).begin(), (X).end());
  X.erase(unique((X).begin(), (X).end()), X.end());
  for (int i = 0, _i = (((int)(X).size())); i < _i; i++) x[i] = false;
  for (int i = 0, _i = (N); i < _i; i++) {
    A[i] = lower_bound((X).begin(), (X).end(), A[i]) - X.begin();
    if (not x[A[i]]) x[A[i]] = true, l[A[i]] = i;
    r[A[i]] = i;
  }
  int mv = 0;
  int ce = -1;
  int cc = 0;
  for (int i = 0, _i = (((int)(X).size())); i < _i; i++) {
    if (ce <= l[i]) {
      cc++;
    } else {
      mv = max(mv, cc);
      cc = 1;
    }
    ce = r[i];
  }
  mv = max(mv, cc);
  cout << ((int)(X).size()) - mv << '\n';
}
int main(void) {
  ios_base::sync_with_stdio(false);
  cin.tie(nullptr);
  int Q;
  cin >> Q;
  while (Q--) solve();
}
