#include <bits/stdc++.h>
using namespace std;
void run();
signed main() {
  ios::sync_with_stdio(0);
  cin.tie(0);
  cout.tie(0);
  run();
  return 0;
}
void solve() {
  int n, inf = 2e9;
  cin >> n;
  vector<pair<int, int>> data(n, make_pair(inf, -inf));
  for (int i = 0; i < n; i++) {
    int a;
    cin >> a;
    a--;
    data[a].first = min(data[a].first, i);
    data[a].second = max(data[a].second, i);
  }
  int ans = 0, k = 0;
  for (int val = 0, temp = 0, last = -1; val < n; val++) {
    if (data[val] == make_pair(inf, -inf)) {
      continue;
    } else {
      k++;
    }
    if (data[val].first > last) {
      temp++;
      last = data[val].second;
    } else {
      ans = max(ans, temp);
      last = data[val].second;
      temp = 1;
    }
    ans = max(ans, temp);
  }
  cout << k - ans << '\n';
}
void run() {
  int q;
  cin >> q;
  while (q--) {
    solve();
  }
}
