#include <bits/stdc++.h>
using namespace std;
vector<int> a;
int n;
int r[300001] = {0}, l[300001] = {0};
void solve() {
  int lastR = -1;
  for (int i = 1; i <= n; i++) l[i] = r[i] = 0;
  for (int i = 0; i < n; i++) {
    r[a[i]] = i + 1;
    if (l[a[i]] == 0) l[a[i]] = i + 1;
  }
  int ans = 0, cur = 0, kol = 0;
  for (int i = 1; i <= n; i++) {
    if (l[i] == 0) continue;
    kol++;
    if (l[i] < lastR) cur = 0;
    cur++;
    lastR = r[i];
    ans = max(ans, cur);
  }
  cout << kol - ans << '\n';
}
void init() {
  cin >> n;
  a.resize(n, 0);
  for (int i = 0; i < n; i++) cin >> a[i];
}
int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(0);
  cout.tie(0);
  int q;
  cin >> q;
  while (q--) {
    init();
    solve();
  }
  return 0;
}
