#include <bits/stdc++.h>
using namespace std;
const long long INF = 1e13;
void solve() {
  long long n;
  cin >> n;
  vector<pair<long long, long long> > a(n);
  for (long long i = 0; i < n; i++) {
    cin >> a[i].first;
    a[i].second = i;
  }
  sort(a.begin(), a.end());
  long long cnt = 1, ans = 1, w = 1;
  for (long long i = 1; i < n; i++) {
    if (a[i].first == a[i - 1].first) continue;
    w++;
    if (a[i].second > a[i - 1].second)
      cnt++;
    else {
      ans = max(ans, cnt);
      cnt = 1;
    }
  }
  ans = max(ans, cnt);
  cout << w - ans << "\n";
}
signed main() {
  ios_base::sync_with_stdio(0);
  cin.tie(0);
  cout.tie(0);
  long long q;
  cin >> q;
  while (q--) solve();
}
