#include <bits/stdc++.h>
using namespace std;
int main() {
  cin.tie(nullptr);
  ios::sync_with_stdio(false);
  int q;
  cin >> q;
  while (q--) {
    int n;
    cin >> n;
    vector<int> a(n);
    for (int i = 0; i < n; i++) cin >> a[i];
    set<int> s;
    map<int, int> m;
    for (int i = 0; i < n; i++) s.insert(a[i]);
    for (auto &x : s) {
      int c = m.size();
      m[x] = c;
    }
    int sz = m.size();
    vector<int> f(sz, n + 1), e(sz, 0);
    for (int i = 0; i < n; i++) {
      int c = m[a[i]];
      f[c] = min(f[c], i);
      e[c] = max(e[c], i);
    }
    int maxi = 0;
    for (int i = 0; i < sz;) {
      int cnt = 0;
      int curr = -1;
      while (i < sz && f[i] > curr) {
        curr = e[i];
        cnt++;
        i++;
      }
      maxi = max(cnt, maxi);
    }
    cout << (int)m.size() - maxi << "\n";
  }
  return 0;
}
