#include <bits/stdc++.h>
using namespace std;
const int MAXN = 300100;
pair<int, int> range[MAXN];
int memo[MAXN];
signed main() {
  memset(range, -1, sizeof range);
  int q;
  cin >> q;
  while (q--) {
    int n;
    cin >> n;
    set<int> s;
    for (int i = 1; i <= n; i++) {
      int x;
      cin >> x;
      s.insert(x);
      if (range[x].first == -1)
        range[x] = {i, i};
      else
        range[x].second = i;
    }
    vector<int> vec;
    for (auto i : s) vec.push_back(i);
    for (int i = (int)vec.size() - 1; i >= 0; i--) {
      int at = vec[i];
      memo[i] = i;
      if (i < (int)vec.size() - 1 && range[at].second < range[vec[i + 1]].first)
        memo[i] = memo[i + 1];
    }
    int h = s.size();
    int resp = MAXN;
    for (int i = 0; i < vec.size(); i++) {
      resp = min(resp, h - (memo[i] - i + 1));
    }
    cout << resp << endl;
    for (auto i : s) range[i] = {-1, -1};
  }
}
