#include <bits/stdc++.h>
using namespace std;
const long long N = 1e5 + 5;
long long n;
long long a[N], store[N];
int32_t main() {
  ios::sync_with_stdio(0);
  cin.tie(0);
  cout.tie(0);
  ;
  long long t;
  cin >> t;
  while (t--) {
    cin >> n;
    map<long long, long long> comp;
    for (long long i = 1; i <= n; i++) {
      cin >> a[i];
      comp[a[i]];
      store[i] = 0;
    }
    long long ctr = 0;
    for (auto &it : comp) it.second = ++ctr;
    long long ans = 0;
    for (long long i = 1; i <= n; i++) {
      a[i] = comp[a[i]];
      store[a[i]] = store[a[i] - 1] + 1;
      ans = max(ans, store[a[i]]);
    }
    cout << n - ans << "\n";
  }
  return 0;
}
