#include <bits/stdc++.h>
using namespace std;
const int maxn = 3e3 + 5;
int n, m, k;
int a[maxn], c[maxn], f[maxn], pos[maxn];
int ans, tmp, cnt;
int flag;
char s[maxn];
bool ok[maxn];
int dp[maxn][maxn];
template <typename T>
inline void read(T &X) {
  X = 0;
  int w = 0;
  char ch = 0;
  while (!isdigit(ch)) {
    w |= ch == '-';
    ch = getchar();
  }
  while (isdigit(ch)) X = (X << 3) + (X << 1) + (ch ^ 48), ch = getchar();
  if (w) X = -X;
}
int cal() {
  int len = 1;
  f[1] = a[0] - 1;
  for (int i = (1); i <= (n); i++) {
    if (a[i] > f[len])
      f[++len] = a[i];
    else
      f[lower_bound(f + 1, f + len + 1, a[i]) - f] = a[i];
  }
  return len;
}
int main() {
  int T, cas = 1;
  read(T);
  while (T--) {
    read(n);
    for (int i = (1); i <= (n); i++) {
      read(a[i]);
      c[i] = a[i];
      pos[i] = -1;
    }
    sort(c + 1, c + 1 + n);
    for (int i = (1); i <= (n); i++) {
      a[i] = lower_bound(c + 1, c + 1 + n, a[i]) - c;
      pos[a[i]] = i;
    }
    int ans = 0;
    for (int i = (1); i <= (n); i++) {
      int len = 1, sum = i + 1;
      for (int j = (pos[i] + 1); j <= (n); j++) {
        if (a[j] == sum) {
          len++;
          sum++;
        }
      }
      ans = max(ans, len);
    }
    printf("%d\n", n - ans);
  }
  return 0;
}
