#include <bits/stdc++.h>
using namespace std;
const int MAX = 1e5 + 3;
const long long MOD = 1e9 + 7;
void solve() {
  int n, k = 0;
  cin >> n;
  set<int> st;
  int a[n + 1];
  int ara[n + 1];
  for (int i = 1; i <= n; i++) {
    cin >> a[i];
    st.insert(a[i]);
  }
  for (auto it : st) {
    ara[it] = ++k;
  }
  int L[k + 1], R[k + 1];
  for (int i = 1; i <= n; i++) {
    R[ara[a[i]]] = i;
  }
  for (int i = n; i >= 1; i--) {
    L[ara[a[i]]] = i;
  }
  int mx = 1, cur = 1;
  for (int i = k - 1; i >= 1; i--) {
    if (R[i] < L[i + 1])
      cur++;
    else
      cur = 1;
    mx = max(mx, cur);
  }
  cout << k - mx << "\n";
}
int main() {
  ios::sync_with_stdio(false);
  cin.tie(0);
  cout.tie(0);
  int tc;
  cin >> tc;
  while (tc--) {
    solve();
  }
}
