#include <bits/stdc++.h>
using namespace std;
long long sortedLen(vector<pair<long long, long long> > &v) {
  sort(v.begin(), v.end());
  long long mx = 0;
  long long len = 1;
  for (long long i = 1; i < (int)v.size(); i++) {
    if (v[i].second < v[i - 1].second) {
      mx = max(mx, len);
      len = 1;
    } else
      len++;
  }
  mx = max(mx, len);
  return mx;
}
void syntex_error() {
  long long i, j, k, l, r, n, m, a, b, c, x, y, z, sum = 0LL;
  vector<pair<long long, long long> > v;
  cin >> n;
  for (i = 0; i < n; i++) {
    cin >> x;
    v.push_back({x, i});
  }
  cout << n - sortedLen(v) << "\n";
}
int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
  long long TC = 1LL;
  cin >> TC;
  while (TC--) {
    syntex_error();
  }
  return 0;
}
