#include <bits/stdc++.h>
#pragma GCC optimize("O3")
#pragma GCC optimize("unroll-loops")
using namespace std;
using ll = long long;
using db = long double;
using ii = pair<int, int>;
const int N = 1e6 + 5, LG = 19, MOD = 998244353;
const int SQ = 225;
int32_t main() {
  ios_base::sync_with_stdio(0);
  cin.tie(0);
  int t;
  cin >> t;
  while (t--) {
    int n;
    cin >> n;
    vector<ii> v;
    for (int i = 0, x; i < n; i++) {
      cin >> x;
      v.push_back({x, i});
    }
    sort(v.begin(), v.end());
    int mx = 0;
    for (int i = 0; i < n; i++) {
      int cnt = 1;
      for (int j = i + 1; j < n && v[j].second >= v[j - 1].second; j++) cnt++;
      mx = max(mx, cnt);
    }
    cout << n - mx << '\n';
  }
  return 0;
}
