#include <bits/stdc++.h>
using namespace std;
void sol() {
  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 mx = 1, cur = 1;
  for (long long i = 1; i < n; i++) {
    if (a[i].second > a[i - 1].second)
      cur++;
    else {
      mx = max(mx, cur);
      cur = 1;
    }
  }
  mx = max(mx, cur);
  cout << n - mx << "\n";
}
int32_t main() {
  long long t;
  cin >> t;
  while (t--) sol();
}
