#include <bits/stdc++.h>
using namespace std;
int a[10000];
int invA[10000];
pair<int, int> b[10000];
void testCase() {
  int n;
  cin >> n;
  for (int i = 0; i < n; i++) cin >> a[i];
  for (int i = 0; i < n; i++) b[i] = {a[i], i};
  sort(b, b + n);
  for (int i = 0; i < n; i++) a[b[i].second] = i + 1;
  for (int i = 0; i < n; i++) invA[a[i]] = i;
  int best = 1e9;
  for (int i = 0; i < n; i++) {
    int cost = i;
    int x = i + 1;
    int ketju = 1;
    while (x + 1 <= n && invA[x + 1] > invA[x]) {
      ketju++;
      x++;
    }
    cost += n - i - ketju;
    best = min(best, cost);
  }
  cout << best << "\n";
}
int main() {
  ios_base::sync_with_stdio(0);
  cin.tie(0);
  int t;
  cin >> t;
  for (int i = 0; i < t; i++) {
    testCase();
  }
}
