#include <bits/stdc++.h>
using namespace std;
int a[300005];
set<int> ele;
int s[300005];
int e[300005];
set<pair<int, pair<int, int> > > p;
int main() {
  int q;
  cin >> q;
  while (q--) {
    int n;
    cin >> n;
    for (int i = 1; i <= n; i++) {
      cin >> a[i];
      ele.insert(a[i]);
      if (s[a[i]] == 0) {
        s[a[i]] = i;
      }
      e[a[i]] = max(e[a[i]], i);
    }
    int cur = 0;
    int ans = 0;
    int cure = 0;
    for (auto u : ele) {
      if (s[u] > cure) {
        cure = e[u];
        cur++;
      } else {
        cure = e[u];
        cur = 1;
      }
      ans = max(ans, cur);
    }
    for (auto u : ele) {
      s[u] = 0;
      e[u] = 0;
    }
    cout << (ele.size() - ans) << endl;
    p.clear();
    ele.clear();
  }
}
