#include <bits/stdc++.h>
using namespace std;
map<long long, long long> dp;
long long n;
int main() {
  ios_base::sync_with_stdio(0);
  ;
  long long T;
  cin >> T;
  while (T--) {
    cin >> n;
    long long maxx = 0;
    dp.clear();
    vector<long long> massiv;
    vector<long long> num;
    for (long long i = 1; i <= n; i++) {
      long long x;
      cin >> x;
      massiv.push_back(x);
      num.push_back(x);
    }
    sort((num).begin(), (num).end());
    num.erase(unique((num).begin(), (num).end()), num.end());
    for (long long i = 0; i < n; i++) {
      long long el =
          upper_bound((num).begin(), (num).end(), massiv[i]) - num.begin();
      dp[el] = dp[el - 1] + 1;
      maxx = max(maxx, dp[el]);
    }
    cout << n - maxx << "\n";
  }
  return 0;
}
