#include <bits/stdc++.h>
using namespace std;
int main() {
  ios::sync_with_stdio(0);
  cin.tie(0);
  ;
  long long t;
  cin >> t;
  while (t--) {
    long long n;
    cin >> n;
    vector<pair<long long, long long>> value;
    for (long long i = 0; i < n; i++) {
      long long x;
      cin >> x;
      value.push_back({x, i});
    }
    sort(value.begin(), value.end());
    long long ans = 0;
    long long t = 1;
    for (long long i = 0; i < n; i++) {
      if (i == 0 || value[i].second < value[i - 1].second)
        t = 1;
      else
        t++;
      ans = max(ans, t);
    }
    cout << n - ans << "\n";
  }
  return 0;
}
