#include <bits/stdc++.h>
using namespace std;
const int N = 300010;
int n, arr[N], l[N], r[N];
int L, R, val, seg[4 * N], lazy[4 * N];
int ans = 0, j, cur, all = 0;
void solve() {
  scanf("%d", &n);
  for (int i = 1; i <= n; i++) {
    l[i] = 1000000010;
    r[i] = -1000000010;
  }
  for (int i = 0; i < n; i++) {
    scanf("%d", &arr[i]);
    l[arr[i]] = min(l[arr[i]], i);
    r[arr[i]] = max(r[arr[i]], i);
  }
  ans = cur = all = 0;
  j = -1;
  for (int i = 1; i <= n; i++) {
    if (l[i] > r[i]) continue;
    all++;
    cur++;
    if (l[i] < j) {
      cur = 1;
    }
    j = r[i];
    ans = max(ans, cur);
  }
  ans = all - ans;
  printf("%d\n", ans);
}
int main() {
  int t;
  scanf("%d", &t);
  while (t--) {
    solve();
  }
  return 0;
}
