#include <bits/stdc++.h>
using namespace std;
int main() {
  ios_base::sync_with_stdio(false), cin.tie(0), cout.tie(0);
  ;
  int q;
  cin >> q;
  while (q--) {
    int n;
    cin >> n;
    set<int> s;
    vector<int> f(n + 1, 0), l(n + 1, 0);
    for (int i = 1; i <= n; i++) {
      int x;
      cin >> x;
      if (f[x] == 0) f[x] = i;
      l[x] = i;
      s.insert(x);
    }
    int ans = n;
    int prev = -1;
    int last = -1;
    for (auto &it : s) {
      if (prev == -1)
        prev = 1;
      else if (l[last] > f[it])
        prev = 1;
      else
        prev++;
      ans = min(ans, (int)s.size() - prev);
      last = it;
    }
    cout << ans << "\n";
  }
}
