#include <bits/stdc++.h>
using namespace std;
const double eps = 1e-9;
const int N = (int)2e6 + 5;
int n, q, a[N], l[N], r[N];
int main() {
  for (scanf("%d", &q); q--;) {
    scanf("%d", &n);
    for (int i = 1; i <= n; ++i) scanf("%d", a + i);
    for (int i = 1; i <= n; ++i) l[i] = n + 1, r[i] = -1;
    for (int i = 1; i <= n; ++i)
      l[a[i]] = min(l[a[i]], i), r[a[i]] = max(r[a[i]], i);
    int mx = 0, now = 0, pos = -1, typ = 0;
    for (int i = 1; i <= n; ++i)
      if (r[i] != -1) {
        ++typ;
        if (now == 0) {
          now = 1;
          pos = r[i];
        } else {
          if (pos < l[i]) {
            pos = r[i];
            ++now;
          } else
            pos = r[i], now = 1;
        }
        mx = max(mx, now);
      }
    printf("%d\n", typ - mx);
  }
  return 0;
}
