#include <bits/stdc++.h>
using namespace std;
int dx[] = {-1, 0, 1, 0, 0}, dy[] = {0, 1, 0, -1, 0};
const int INF = 0x3f3f3f3f;
const long long IINF = 0x3f3f3f3f3f3f3f3f;
const int N = 3010;
int a[N], b[N];
int f[N];
int main() {
  int T;
  cin >> T;
  while (T--) {
    int n;
    cin >> n;
    for (int i = 1; i <= n; i++) cin >> a[i], b[i] = a[i], f[i] = 0;
    sort(b + 1, b + n + 1);
    for (int i = 1; i <= n; i++) a[i] = lower_bound(b + 1, b + n + 1, a[i]) - b;
    int res = 0;
    for (int i = 1; i <= n; i++) {
      f[a[i]] = f[a[i] - 1] + 1;
      res = max(res, f[a[i]]);
    }
    cout << n - res << endl;
  }
}
