#include <bits/stdc++.h>
using namespace std;
const int INF = 1e9;
int main() {
  int t, n;
  cin >> t;
  for (int tc = 0; tc < t; ++tc) {
    cin >> n;
    int a[n];
    int l[n], r[n];
    int dp[n];
    for (int i = 0; i < n; ++i) {
      l[i] = INF;
      r[i] = -INF;
      dp[i] = 0;
    }
    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(res, int(v.size()) - dp[i]);
    }
    cout << res << endl;
  }
  return 0;
}
