#include <bits/stdc++.h>
using namespace std;
const int mod = 1e4 + 7;
const int N = 2e6 + 10;
const int inf = 0x7f7f7f7f;
long long gcd(long long a, long long b) { return b == 0 ? a : gcd(b, a % b); }
long long lcm(long long a, long long b) { return a * (b / gcd(a, b)); }
template <class T>
void read(T &x) {
  char c;
  bool op = 0;
  while (c = getchar(), c < '0' || c > '9')
    if (c == '-') op = 1;
  x = c - '0';
  while (c = getchar(), c >= '0' && c <= '9') x = x * 10 + c - '0';
  if (op) x = -x;
}
template <class T>
void write(T x) {
  if (x < 0) x = -x, putchar('-');
  if (x >= 10) write(x / 10);
  putchar('0' + x % 10);
}
int a[5000], b[5000];
int main() {
  std::ios::sync_with_stdio(false);
  int t;
  cin >> t;
  while (t--) {
    int n;
    cin >> n;
    for (int i = 1; i <= n; i++) cin >> a[i], b[i] = a[i];
    sort(b + 1, b + 1 + n);
    int mx = 0;
    for (int i = 1; i <= n; i++) {
      int cnt = 0;
      for (int j = 1, k = i; j <= n && k <= n; j++) {
        if (b[k] == a[j]) {
          k++;
          cnt++;
        }
      }
      mx = max(mx, cnt);
    }
    int ans = n - mx;
    cout << ans << endl;
  }
  return 0;
}
