#include <bits/stdc++.h>
using namespace std;
const int mxn = 3005;
int n;
pair<int, int> a[mxn];
void solve() {
  cin >> n;
  for (int i = 1; i <= n; ++i) {
    cin >> a[i].first;
    a[i].second = i;
  }
  int ans = 1, dem = 1;
  sort(a + 1, a + n + 1);
  for (int i = 2; i <= n; ++i) {
    if (a[i - 1].second < a[i].second)
      ++dem;
    else
      dem = 1;
    ans = max(ans, dem);
  }
  cout << n - ans << '\n';
}
int main() {
  ios_base::sync_with_stdio(0);
  cin.tie(0);
  if (fopen("codeforces"
            ".inp",
            "r")) {
    freopen(
        "codeforces"
        ".inp",
        "r", stdin);
    freopen(
        "codeforces"
        ".out",
        "w", stdout);
  }
  int t;
  cin >> t;
  while (t--) solve();
}
