#include <bits/stdc++.h>
using namespace std;
const int inf = 1e9;
void solve() {
  int n;
  cin >> n;
  vector<int> arr(n);
  for (int &i : arr) cin >> i;
  auto brr = arr;
  sort(brr.begin(), brr.end());
  for (int &i : arr)
    i = lower_bound(brr.begin(), brr.end(), i) - brr.begin() + 1;
  vector<int> dp(n + 1);
  for (int &i : arr) {
    dp[i] = 1 + dp[i - 1];
  }
  cout << n - *max_element(dp.begin(), dp.end()) << "\n";
}
int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
  cout.tie(NULL);
  ;
  std::cout << std::fixed << std::setprecision(20);
  ;
  int t = 1;
  cin >> t;
  for (int i = 1; i <= t; i++) {
    solve();
  }
}
