#include <bits/stdc++.h>
using namespace std;
void dout() { cerr << "\n"; }
template <typename Head, typename... Tail>
void dout(Head H, Tail... T) {
  cerr << H << ' ';
  dout(T...);
}
mt19937 rng(chrono::steady_clock::now().time_since_epoch().count());
const long long N = 3e5 + 7;
long long n, a[N], dp[N], l[N], r[N], cnt[N];
map<long long, long long> val;
set<long long> st;
void cleanup() {
  val = {};
  st.clear();
  for (long long i = 1; i <= n; i++) {
    a[i] = l[i] = r[i] = cnt[i] = dp[i] = 0;
  }
}
void solve(long long tc) {
  cin >> n;
  for (long long i = 1; i <= n; i++) {
    cin >> a[i];
    st.insert(a[i]);
  }
  long long cur = 0;
  for (auto i : st) {
    val[i] = ++cur;
  }
  for (long long i = 1; i <= n; i++) {
    a[i] = val[a[i]];
    if (!l[a[i]]) {
      l[a[i]] = i;
    }
    r[a[i]] = i;
    cnt[a[i]]++;
  }
  for (long long i = 1; i <= n; i++) {
    dp[a[i]] = 1;
  }
  for (long long i = 1; i <= n; i++) {
    if (r[a[i] - 1] < l[a[i]]) {
      dp[a[i]] = max(dp[a[i]], dp[a[i] - 1] + 1);
    }
  }
  long long res = (long long)1e18;
  for (long long i = 1; i <= n; i++) {
    res = min(res, cur - dp[a[i]]);
  }
  cout << res << "\n";
}
signed main() {
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
  long long tc = 1;
  cin >> tc;
  for (long long i = 0; i < tc; i++) {
    solve(i);
    cleanup();
  }
}
