#include <bits/stdc++.h>
using namespace std;
const int N = 300005;
int T, n, l[N], r[N], ans, cnt;
int main() {
  scanf("%d", &T);
  while (T--) {
    scanf("%d", &n);
    for (int i = 1; i <= n; i++) l[i] = 0;
    for (int i = 1; i <= n; i++) {
      int x;
      scanf("%d", &x);
      if (!l[x]) l[x] = i;
      r[x] = i;
    }
    ans = 0, cnt = 0;
    int mx = 0, cur = 0;
    for (int i = 1; i <= n; i++) {
      if (l[i]) cnt++;
      if (l[i] > mx)
        cur++, mx = r[i];
      else if (l[i])
        cur = 1, mx = r[i];
      ans = max(ans, cur);
    }
    printf("%d\n", cnt - ans);
  }
  return 0;
}
