#include <bits/stdc++.h>
using namespace std;
int main() {
  cin.tie(0);
  ios::sync_with_stdio(false);
  int q;
  cin >> q;
  for (int _ = 0; _ < (q); _++) {
    int n;
    cin >> n;
    vector<int> a(n);
    for (int i = 0; i < (n); i++) cin >> a[i], a[i]--;
    vector<int> l(n, -1), r(n, -1);
    for (int i = 0; i < (n); i++) {
      if (l[a[i]] == -1) l[a[i]] = i;
      r[a[i]] = i;
    }
    int sz = 0;
    for (int i = 0; i < (n); i++) {
      if (l[i] != -1) {
        l[sz] = l[i];
        r[sz] = r[i];
        sz++;
      }
    }
    int rb = 1;
    int ans = 0;
    for (int i = 0; i < (sz); i++) {
      if (i == rb) rb++;
      while (rb < sz) {
        if (r[rb - 1] > l[rb]) break;
        rb++;
      }
      ans = max(ans, rb - i);
    }
    cout << sz - ans << endl;
  }
  return 0;
}
