#include <bits/stdc++.h>
using namespace std;
const int maxn = 3e5 + 10;
int r[maxn], l[maxn];
int main() {
  int q;
  scanf("%d", &q);
  while (q--) {
    int n;
    scanf("%d", &n);
    for (int i = 1; i <= n; i++) {
      l[i] = r[i] = 0;
    }
    int tot = 0;
    for (int i = 1; i <= n; i++) {
      int temp;
      scanf("%d", &temp);
      if (!l[temp]) {
        tot++;
        l[temp] = i;
      }
      r[temp] = i;
    }
    int cnt = 0;
    int pre;
    int ans = 0;
    for (int i = 1; i <= n; i++) {
      if (l[i]) {
        if (cnt == 0) {
          cnt = 1;
          pre = r[i];
        } else {
          if (l[i] > pre) {
            cnt++;
            pre = r[i];
          } else {
            cnt = 1;
            pre = r[i];
          }
        }
        ans = max(ans, cnt);
      }
    }
    printf("%d\n", tot - ans);
  }
}
