#include <bits/stdc++.h>
using namespace std;
const int maxn = 3e5 + 5;
int n, m;
int i, j, k;
int q;
int l[maxn], r[maxn], a[maxn];
int main() {
  scanf("%d", &q);
  while (q--) {
    scanf("%d", &n);
    for (i = 1; i <= n; i++) l[i] = r[i] = 0;
    for (i = 1; i <= n; i++) {
      scanf("%d", &a[i]);
      if (!l[a[i]]) l[a[i]] = i;
      r[a[i]] = i;
    }
    int cnt = 0, last = 0, sum = 0, res = 0;
    for (i = 1; i <= n; i++) {
      if (l[i]) {
        if (last < l[i])
          cnt++;
        else
          cnt = 1;
        res = max(cnt, res);
        ++sum;
        last = r[i];
      }
    }
    printf("%d\n", sum - res);
  }
  return 0;
}
