#include <bits/stdc++.h>
using namespace std;
template <class T>
inline void read(T &x) {
  x = 0;
  char c = getchar();
  int f = 1;
  while (!isdigit(c)) {
    if (c == '-') f = -1;
    c = getchar();
  }
  while (isdigit(c)) {
    x = x * 10 + c - '0';
    c = getchar();
  }
  x *= f;
}
template <class T>
inline void umin(T &x, T y) {
  x = x < y ? x : y;
}
template <class T>
inline void umax(T &x, T y) {
  x = x > y ? x : y;
}
inline unsigned int R() {
  static unsigned int seed = 416;
  return seed ^= seed >> 5, seed ^= seed << 17, seed ^= seed >> 13;
}
const int N = 377677;
int n, a[N], bg[N], ed[N], vis[N];
int main() {
  int T;
  read(T);
  while (T--) {
    read(n);
    for (register int i = (1); i <= (n); i++) vis[i] = 0;
    for (register int i = (1); i <= (n); i++) read(a[i]), vis[a[i]] = 1;
    for (register int i = (1); i <= (n); i++) vis[i] += vis[i - 1];
    for (register int i = (1); i <= (n); i++) a[i] = vis[a[i]];
    int tot = vis[n], res = 0;
    for (register int i = (1); i <= (n); i++) ed[a[i]] = i;
    for (register int i = (n); i >= (1); i--) bg[a[i]] = i;
    for (int i = 1, j = i; i <= tot; i++) {
      umax(j, i);
      while (j + 1 <= tot && ed[j] < bg[j + 1]) j++;
      umax(res, j - i + 1);
    }
    printf("%d\n", tot - res);
  }
  return 0;
}
