#include <bits/stdc++.h>
using namespace std;
#pragma GCC optimize("Ofast")
#pragma GCC target("avx,avx2,fma")
#pragma GCC optimization("unroll-loops")
void tracer(istream_iterator<string> it) {}
template <typename T, typename... Args>
void tracer(istream_iterator<string> it, T a, Args... args) {
  cerr << *it << " = " << a << " | ";
  tracer(++it, args...);
}
const long long MOD = 1e9 + 7;
const double err = 1e-10;
const int N = 2e5 + 5;
void solve() {
  int n;
  cin >> n;
  vector<int> V(n);
  vector<int> first(n + 1, 2e9), last(n + 1, -1);
  for (int i = 0; i < n; i++) {
    cin >> V[i];
    first[V[i]] = min(first[V[i]], i);
    last[V[i]] = i;
  }
  vector<int> util = V;
  sort((util).begin(), (util).end());
  util.erase(unique((util).begin(), (util).end()), util.end());
  int mxcnt = 0, diff = ((int)(util).size()), cnt = 0, prevnd = -1;
  for (int i = 0; i < ((int)(util).size()); i++) {
    if (first[util[i]] > prevnd) {
      cnt++;
    } else {
      cnt = 1;
    }
    prevnd = last[util[i]];
    mxcnt = max(mxcnt, cnt);
  }
  cout << diff - mxcnt << endl;
}
int32_t main() {
  ios_base::sync_with_stdio(0);
  cin.tie(0);
  int T = 1;
  cin >> T;
  while (T--) solve();
  return 0;
}
