#include <bits/stdc++.h>
using namespace std;
template <typename T>
inline void read(T &x) {
  bool b = 0;
  char c;
  while (!isdigit(c = getchar()) && c != '-')
    ;
  if (c == '-') c = getchar(), b = 1;
  x = c - 48;
  while (isdigit(c = getchar())) x = (x << 3) + (x << 1) + c - 48;
  if (b) x = -x;
}
template <typename T>
inline void wrip(T x) {
  if (x > 9) wrip(x / 10);
  putchar(x % 10 + 48);
}
template <typename T>
inline void write(T x) {
  if (x < 0) putchar('-'), x = -x;
  wrip(x);
  putchar(' ');
}
template <typename T>
inline void writeln(T x) {
  if (x < 0) putchar('-'), x = -x;
  wrip(x);
  putchar('\n');
}
inline char readch() {
  char c;
  do c = getchar();
  while (c == '\n' || c == ' ');
  return c;
}
inline void reads(string &s) {
  char c;
  while ((c = getchar()) == ' ' || c == '\n')
    ;
  s = c;
  while ((c = getchar()) != ' ' && c != '\n') s += c;
}
inline void getlines(string &s) {
  char c;
  while ((c = getchar()) == '\n')
    ;
  s = c;
  while ((c = getchar()) != '\n') s += c;
}
const long double pi = acos(-1);
const int inf = 1e9, mod = 998244353, N = 2e5 + 11;
int n, pos[N], l[N], r[N];
pair<int, int> a[N];
int main() {
  int ntest = 1;
  read(ntest);
  while (ntest--) {
    read(n);
    for (int i = 1; i <= n; i++) {
      read(a[i].first);
      a[i].second = i;
    }
    sort(a + 1, a + n + 1);
    int res = n;
    for (int i = 1; i <= n; i++) {
      int last = a[i].second;
      for (int j = i; j <= n; j++) {
        if (a[j].second < last) break;
        last = a[j].second;
        res = min(res, n - j + i - 1);
      }
    }
    cout << res << '\n';
  }
}
