#include <bits/stdc++.h>
using namespace std;
const int maxn = 3e5 + 5;
const int inf = 1e9 + 1;
int n, q, a[maxn], b[maxn], maxx[maxn], minn[maxn], dp[maxn];
int main() {
  scanf("%d", &q);
  while (q--) {
    scanf("%d", &n);
    for (int i = 1; i <= n; ++i) scanf("%d", &a[i]), b[i] = a[i];
    sort(b + 1, b + 1 + n);
    int size = unique(b + 1, b + 1 + n) - b - 1;
    for (int i = 1; i <= n; ++i)
      a[i] = lower_bound(b + 1, b + 1 + size, a[i]) - b, minn[i] = inf,
      maxx[i] = -inf;
    for (int i = 1; i <= n; ++i)
      minn[a[i]] = min(minn[a[i]], i), maxx[a[i]] = max(maxx[a[i]], i);
    int ans = 0;
    for (int i = 1; i <= size; ++i) dp[i] = 1;
    for (int i = 1; i <= size; ++i) {
      if (minn[i] > maxx[i - 1]) dp[i] = dp[i - 1] + 1;
      ans = max(ans, dp[i]);
    }
    printf("%d\n", size - ans);
  }
}
