#include <bits/stdc++.h>
using namespace std;
int main() {
  ios::sync_with_stdio(0);
  cout.tie(0);
  cin.tie(0);
  int q;
  cin >> q;
  while (q--) {
    int n;
    cin >> n;
    vector<int> a(n), d;
    set<int> s;
    map<int, int> f, l;
    for (int i = 0; i < n; i++) {
      cin >> a[i];
      s.insert(a[i]);
      if (f.count(a[i]) == 0) {
        f[a[i]] = i;
        l[a[i]] = i;
      } else
        l[a[i]] = i;
    }
    for (auto& i : s) d.push_back(i);
    int t = 1, t1 = 0;
    for (int i = 1; i < d.size(); i++) {
      if (l[d[i - 1]] < f[d[i]])
        t += 1;
      else {
        t1 = max(t, t1), t = 1;
      }
    }
    t1 = max(t, t1);
    cout << d.size() - t1 << "\n";
  }
}
