#include <bits/stdc++.h>
using namespace std;
const int MAX = 3010;
int t;
int n;
int x;
int arr[MAX];
int arrx[MAX];
vector<pair<int, int>> v;
int main() {
  cin >> t;
  while (t--) {
    cin >> n;
    v.clear();
    for (int i = 0; i < MAX; i++) {
      arr[i] = 0;
      arrx[i] = 0;
    }
    for (int i = 0; i < n; i++) {
      cin >> x;
      v.push_back({x, i});
    }
    sort(v.begin(), v.end());
    for (int i = 0; i < n; i++) {
      arrx[v[i].second] = i + 1;
    }
    int sol = -1e9;
    for (int i = 0; i < n; i++) {
      x = arrx[i];
      arr[x] = arr[x - 1] + 1;
      sol = max(sol, arr[x]);
    }
    cout << n - sol << "\n";
  }
  return 0;
}
