#include <bits/stdc++.h>
using namespace std;
const int N = 3e5 + 5;
int a[N], l[N], r[N];
int n;
bool vis[N];
int main() {
  int T;
  scanf("%d", &T);
  while (T--) {
    scanf("%d", &n);
    memset(vis, 0, (n + 1) * sizeof(char));
    memset(l, 0, (n + 1) * sizeof(int));
    int t;
    for (int i = 1; i <= n; i++) {
      scanf("%d", &t);
      a[i] = t;
      if (!vis[t]) {
        vis[t] = 1;
        l[t] = i;
      }
      r[t] = i;
    }
    int maxlen, len, num, last;
    maxlen = len = num = last = 0;
    l[0] = r[0] = 0;
    for (int i = 1; i <= n; i++) {
      if (!l[i]) continue;
      if (r[last] < l[i]) {
        len++;
      } else {
        maxlen = max(len, maxlen);
        len = 1;
      }
      last = i;
      num++;
    }
    maxlen = max(len, maxlen);
    printf("%d\n", num - maxlen);
  }
  return 0;
}
