#include <bits/stdc++.h>
using namespace std;
const long double pi = acos(-1);
const long long inf = 1LL << 30;
const double eps = 1e-9;
const long long mod = 1e9 + 7;
const int N = 300300;
int a[N], mn_id[N], mx_id[N];
int pre[N], suf[N];
vector<int> v;
set<int> tmp;
int main() {
  int t;
  scanf("%d", &t);
  while (t--) {
    int n;
    scanf("%d", &n);
    tmp.clear();
    v.clear();
    for (int i = 1; i <= n; i++) {
      scanf("%d", &a[i]);
      mx_id[a[i]] = i;
      tmp.insert(a[i]);
    }
    for (int i = n; i > 0; i--) mn_id[a[i]] = i;
    for (auto x : tmp) v.push_back(x);
    suf[v.size() - 1] = 0;
    for (int i = v.size() - 2; i >= 0; i--) {
      if (mn_id[v[i + 1]] < mx_id[v[i]])
        suf[i] = v.size() - i - 1;
      else
        suf[i] = suf[i + 1];
    }
    int ans = suf[0];
    pre[0] = 0;
    for (int i = 1; i < v.size(); i++) {
      if (mx_id[v[i - 1]] > mn_id[v[i]])
        pre[i] = i;
      else
        pre[i] = pre[i - 1];
      ans = min(ans, pre[i] + suf[i]);
    }
    printf("%d\n", ans);
  }
}
