#include <bits/stdc++.h>
using namespace std;
int inf = 1e8;
int main() {
  ios::sync_with_stdio(0);
  cin.tie(0);
  cout.tie(0);
  int q;
  cin >> q;
  for (int p = 0; p < q; p++) {
    int n;
    cin >> n;
    vector<int> a(n);
    int t = 0;
    vector<int> b(n + 1, 0);
    vector<int> dp(n + 1, 0);
    vector<int> l(n + 1, inf);
    vector<int> r(n + 1, -inf);
    vector<int> v;
    for (int i = 0; i < n; i++) {
      cin >> a[i];
      a[i]--;
      v.push_back(a[i]);
      l[a[i]] = min(l[a[i]], i);
      r[a[i]] = max(r[a[i]], i);
    }
    sort(v.begin(), v.end());
    v.erase(unique(v.begin(), v.end()), v.end());
    int res = n;
    for (int i = v.size() - 1; i >= 0; i--) {
      if (i + 1 == v.size() || r[v[i]] >= l[v[i + 1]]) {
        dp[i] = 1;
      } else {
        dp[i] = 1 + dp[i + 1];
      }
      res = min(int(v.size()) - dp[i], res);
    }
    cout << res << "\n";
  }
  return 0;
}
