#include <bits/stdc++.h>
using namespace std;
int main() {
  ios::sync_with_stdio(0);
  int q;
  cin >> q;
  while (q--) {
    int n;
    cin >> n;
    vector<int> l(n, -1), r(n, -1);
    for (int i = 0, x; i < n; i++) {
      cin >> x;
      x--;
      if (l[x] == -1) l[x] = i;
      r[x] = i;
    }
    vector<int> v;
    for (int i = 0; i < n; ++i)
      if (l[i] != -1) v.push_back(i);
    int ans = n, j = (int)v.size() - 1;
    for (int i = (int)v.size() - 1; i >= 0; i--) {
      if (i != (int)v.size() - 1 && r[v[i]] > l[v[i + 1]]) j = i;
      ans = min(ans, (int)v.size() - (j - i + 1));
    }
    cout << ans << '\n';
  }
  return 0;
}
