#include <bits/stdc++.h>
using namespace std;
int main(void) {
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
  int q;
  cin >> q;
  while (q--) {
    int n;
    cin >> n;
    vector<int> arr, idx;
    vector<int> mx(n + 5, -1), mn(n + 5, numeric_limits<int>::max() / 2);
    for (int i = 0; i < n; i++) {
      int x;
      cin >> x;
      arr.push_back(x);
      idx.push_back(x);
      mx[x] = max(mx[x], i);
      mn[x] = min(mn[x], i);
    }
    sort(idx.begin(), idx.end());
    idx.resize(distance(idx.begin(), unique(idx.begin(), idx.end())));
    int k = idx.size();
    vector<int> memo(k, 1);
    for (int i = k - 2; i >= 0; i--) {
      if (mx[idx[i]] < mn[idx[i + 1]]) memo[i] = memo[i + 1] + 1;
    }
    int ans = 0;
    for (int i = 0; i < k; i++) ans = max(ans, memo[i]);
    cout << k - ans << '\n';
  }
  return 0;
}
