#include <bits/stdc++.h>
using namespace std;
using ll = long long;
using ull = unsigned long long;
using ld = long double;
using pii = pair<int, int>;
using pll = pair<ll, ll>;
void solve() {
  int n;
  cin >> n;
  vector<int> a(n + 1), l(n + 1, INT_MAX), r(n + 1, INT_MIN);
  for (int i = 0; i < n; i++) {
    int x;
    cin >> x;
    a[x] = true;
    l[x] = min(l[x], i);
    r[x] = max(r[x], i);
  }
  int prev = 0, cnt = 0, ans = 0, total = 0;
  for (int i = 1; i <= n; i++)
    if (a[i]) {
      if (r[prev] < l[i])
        cnt++;
      else
        cnt = 1;
      ans = max(ans, cnt);
      prev = i;
      total++;
    }
  cout << total - ans << endl;
  return;
}
int main() {
  ios::sync_with_stdio(0);
  cin.tie(0);
  cout.tie(0);
  cout << fixed << setprecision(10);
  int t;
  cin >> t;
  while (t--) {
    solve();
  }
  return 0;
}
