#include <bits/stdc++.h>
using namespace std;
template <class A>
void pr(A a) {
  cout << a;
  cout << '\n';
}
template <class A, class B>
void pr(A a, B b) {
  cout << a << ' ';
  pr(b);
}
template <class A, class B, class C>
void pr(A a, B b, C c) {
  cout << a << ' ';
  pr(b, c);
}
template <class A, class B, class C, class D>
void pr(A a, B b, C c, D d) {
  cout << a << ' ';
  pr(b, c, d);
}
template <class A>
void PR(A a, long long n) {
  for (long long i = (long long)(0); i < (long long)(n); i++) {
    if (i) cout << ' ';
    cout << a[i];
  }
  cout << '\n';
}
long long check(long long n, long long m, long long x, long long y) {
  return x >= 0 && x < n && y >= 0 && y < m;
}
const long long MAX = 1e9 + 7, MAXL = 1LL << 61, dx[4] = {-1, 0, 1, 0},
                dy[4] = {0, 1, 0, -1};
vector<pair<long long, long long> > v;
void Main() {
  long long T;
  cin >> T;
  while (T--) {
    v.clear();
    long long n;
    cin >> n;
    long long a[n];
    for (long long i = (long long)(0); i < (long long)(n); i++) cin >> a[i];
    map<long long, pair<long long, long long> > m;
    for (long long i = (long long)(0); i < (long long)(n); i++) {
      if (!m.count(a[i]))
        m[a[i]] = pair<long long, long long>(i, i);
      else
        m[a[i]].second = i;
    }
    for (__typeof((m).begin()) it = (m).begin(); it != (m).end(); it++)
      v.push_back(it->second);
    long long ans = v.size() - 1;
    for (long long i = (long long)(0); i < (long long)(v.size()); i++) {
      long long k = i + 1;
      while (k < v.size()) {
        if (v[k - 1].second < v[k].first)
          k++;
        else
          break;
      }
      ans = min(ans, (long long)v.size() - (k - i));
      i = k - 1;
    }
    pr(ans);
  }
}
int main() {
  ios::sync_with_stdio(0);
  cin.tie(0);
  Main();
  return 0;
}
