#include <bits/stdc++.h>
using namespace std;
void solve() {
  long long int n;
  cin >> n;
  vector<long long int> v(n + 1);
  unordered_map<long long int, long long int> hash;
  for (long long int i = 1; i <= n; i++) {
    cin >> v[i];
    hash[v[i]] = i;
  }
  sort(v.begin(), v.end());
  vector<array<long long int, 2>> pref(n + 2);
  for (long long int i = 1; i <= n; i++) {
    long long int cur = i;
    long long int idx = 0;
    for (long long int j = i; j != 0; j--) {
      if (hash[v[j]] > cur) {
        idx = j;
        break;
      }
      cur = hash[v[j]];
    }
    pref[i][0] = idx;
  }
  for (long long int i = n; i != 0; i--) {
    long long int idx = n + 1;
    long long int cur = i;
    for (long long int j = i; j <= n; j++) {
      if (hash[v[j]] < cur) {
        idx = j;
        break;
      }
      cur = hash[v[j]];
    }
    pref[i][1] = n - idx + 1;
  }
  long long int ans = n - 1;
  for (long long int i = 0; i <= n; i++) {
    ans = min(ans, pref[i][0] + pref[i + 1][1]);
  }
  cout << ans << "\n";
}
int32_t main() {
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
  cout.tie(NULL);
  long long int t;
  cin >> t;
  while (t--) {
    solve();
  }
}
