#include <bits/stdc++.h>
using namespace std;
template <typename T>
void setmax(T& x, T y) {
  x = max(x, y);
}
template <typename T>
void setmin(T& x, T y) {
  x = min(x, y);
}
const double PI = acos(-1.0);
const int INF = 1e9 + 47;
const long long LINF = 1LL * INF * INF;
mt19937 rng(chrono::steady_clock::now().time_since_epoch().count());
int solve() {
  int n;
  cin >> n;
  vector<int> a(n), b(n);
  for (int i = (0); i < (n); ++i) {
    cin >> a[i];
    b[i] = a[i];
  }
  sort(b.begin(), b.end());
  int ans = n - 1;
  for (int l = (0); l < (n); ++l) {
    int posA = l;
    int posB = lower_bound(b.begin(), b.end(), a[l]) - b.begin();
    int bg = posB;
    while (posA < n && posB < n) {
      if (a[posA] == b[posB]) {
        posB++;
      }
      posA++;
    }
    setmin(ans, n - (posB - bg));
  }
  return ans;
}
int main() {
  ios_base::sync_with_stdio(0), cin.tie(0), cout.tie(0);
  int t;
  cin >> t;
  while (t--) {
    cout << solve() << endl;
  }
  cerr << "Time elapsed: " << clock() / (double)CLOCKS_PER_SEC << endl;
  return 0;
}
