#include <bits/stdc++.h>
using namespace std;
const int maxn = 3e5 + 100;
const int mod = 1e9 + 7;
int main() {
  ios_base::sync_with_stdio(false), cin.tie(0);
  int q;
  cin >> q;
  while (q--) {
    int n;
    cin >> n;
    vector<int> v(n, 0), mn(n + 1, mod), mx(n + 1, 0);
    for (int i = 0; i < n; i++) {
      cin >> v[i];
      mn[v[i]] = min(mn[v[i]], i);
      mx[v[i]] = i;
    }
    sort(v.begin(), v.end());
    v.erase(unique(v.begin(), v.end()), v.end());
    n = (int)v.size();
    int ans = 0;
    for (int i = 0; i < (int)v.size(); i++) {
      int j = i;
      while (j + 1 < (int)v.size() && mx[v[j]] < mn[v[j + 1]]) ++j;
      ans = max(ans, j - i + 1);
      i = j;
    }
    cout << (int)v.size() - ans << "\n";
  }
  return 0;
}
