#include <bits/stdc++.h>
using namespace std;
using uint = unsigned int;
using ll = long long;
using ull = unsigned long long;
using pii = pair<int, int>;
using pli = pair<ll, int>;
using pil = pair<int, ll>;
using pll = pair<ll, ll>;
template <typename T>
using vec = vector<T>;
using vi = vec<int>;
using vl = vec<ll>;
template <typename T>
using que = queue<T>;
template <typename T>
using deq = deque<T>;
template <typename T>
T id(T b) {
  return b;
};
template <typename T>
void chmax(T &x, T y) {
  if (x < y) x = y;
}
template <typename T>
void chmin(T &x, T y) {
  if (x > y) x = y;
}
template <typename S, typename K>
bool contains(S &s, K k) {
  return s.find(k) != s.end();
}
void fastio() {
  ios_base::sync_with_stdio(false);
  cin.tie(nullptr);
}
constexpr ll TEN(int n) {
  if (n == 0)
    return 1LL;
  else
    return 10LL * TEN(n - 1);
}
int main() {
  fastio();
  int q;
  cin >> q;
  while (q--) {
    int n;
    cin >> n;
    vi a(n);
    set<int, greater<int>> s;
    for (int i = 0; i < int(n); i++) {
      cin >> a[i];
      s.insert(a[i]);
    }
    map<int, int> mn, mx;
    for (int i = 0; i < int(n); i++) mx[a[i]] = i;
    for (int i = n - 1; i >= 0; i--) mn[a[i]] = i;
    vi l((s).begin(), (s).end());
    vi dp(s.size());
    dp[0] = 1;
    for (int i = 1; i < l.size(); i++) {
      if (mn[l[i - 1]] >= mx[l[i]]) {
        dp[i] = dp[i - 1] + 1;
      } else {
        dp[i] = 1;
      }
    }
    cout << l.size() - *max_element((dp).begin(), (dp).end()) << "\n";
  }
  return 0;
}
