#include <bits/stdc++.h>
using namespace std;
const int maxn = 3e5 + 50;
struct node {
  int l, r, cnt;
} a[maxn];
int n;
vector<int> v;
int vis[maxn];
int main() {
  int t;
  scanf("%d", &t);
  while (t--) {
    while (v.size() != 0) {
      vis[v.back()] = 0;
      v.pop_back();
    }
    scanf("%d", &n);
    int ans = 0;
    for (int i = 1; i <= n; i++) {
      a[i].l = n;
      a[i].r = 1;
      a[i].cnt = 0;
    }
    for (int i = 1; i <= n; i++) {
      int x;
      scanf("%d", &x);
      a[x].l = min(a[x].l, i);
      a[x].r = max(a[x].r, i);
      if (a[x].cnt == 0) ans++;
      a[x].cnt++;
    }
    for (int i = 1; i <= n; i++) {
      if (a[i].cnt != 0) {
        v.push_back(i);
        vis[i] = 1;
      }
    }
    int res = 0;
    int pre = 0;
    int tmp = 0;
    for (int i = 1; i <= n; i++) {
      if (vis[i]) {
        if (a[i].l > pre) {
          pre = a[i].r;
          tmp++;
          res = max(res, tmp);
        } else {
          res = max(res, tmp);
          pre = a[i].r;
          tmp = 1;
        }
      }
    }
    printf("%d\n", ans - res);
  }
}
