#include <bits/stdc++.h>
using namespace std;
using ll = long long;
using ld = long double;
int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(nullptr);
  ll q;
  cin >> q;
  for (ll i = 0; i < q; i++) {
    ll n;
    cin >> n;
    vector<ll> mi(n, n + 1), ma(n, -1);
    for (ll j = 0; j < n; j++) {
      ll x;
      cin >> x;
      x--;
      mi[x] = min(mi[x], j);
      ma[x] = max(ma[x], j);
    }
    vector<pair<ll, ll>> in;
    for (ll i = 0; i < n; i++) {
      if (ma[i] < 0) continue;
      in.push_back({mi[i], ma[i]});
    }
    ll machain = 0;
    ll lastr = -1;
    ll curc = 0;
    for (auto e : in) {
      if (e.first >= lastr) {
        curc++;
      } else {
        curc = 1;
      }
      lastr = e.second;
      machain = max(machain, curc);
    }
    cout << (ll)(in).size() - machain << endl;
  }
}
