#include <bits/stdc++.h>
using namespace std;
const int N = 3e3 + 10;
int a[N], b[N];
int n;
int main() {
  int t;
  cin >> t;
  while (t--) {
    scanf("%d", &n);
    for (int i = 1; i <= n; i++) scanf("%d", &a[i]), b[i] = a[i];
    sort(b + 1, b + 1 + n);
    int maxx = 0;
    for (int i = 1; i <= n; i++) {
      int cnt = 0;
      for (int j = i, k = 1; j <= n && k <= n;) {
        if (b[j] == a[k]) {
          ++j;
          ++cnt;
        }
        ++k;
      }
      maxx = max(maxx, cnt);
    }
    printf("%d\n", n - maxx);
  }
  return 0;
}
