#include <bits/stdc++.h>
using namespace std;
template <class n, class second>
ostream &operator<<(ostream &p, pair<n, second> x) {
  return p << "<" << x.first << ", " << x.second << ">";
}
template <class n>
auto operator<<(ostream &p, n y) ->
    typename enable_if<!is_same<n, string>::value,
                       decltype(y.begin(), p)>::type {
  int o = 0;
  p << "{";
  for (auto c : y) {
    if (o++) p << ", ";
    p << c;
  }
  return p << "}";
}
void dor() { cerr << '\n'; }
template <class n, class... second>
void dor(n p, second... y) {
  cerr << p << " ";
  dor(y...);
}
template <class n, class second>
void mini(n &p, second y) {
  if (p > y) p = y;
}
template <class n, class second>
void maxi(n &p, second y) {
  if (p < y) p = y;
}
const int MXN = 2e5 + 5;
int in[MXN];
int where[MXN];
void solve() {
  int n;
  cin >> n;
  map<int, int> M;
  for (int i = 0; i < (int)(n); ++i) {
    cin >> in[i];
    M[in[i]];
  }
  int id = 1;
  for (auto &i : M) i.second = id++;
  for (int i = 0; i < (int)(n); ++i) {
    in[i] = M[in[i]];
    where[in[i]] = i;
  }
  int res = 0;
  for (int i = (1); i <= (int)(n); ++i) {
    int last = 0;
    for (int j = (i); j <= (int)(n); ++j) {
      if (where[j] < last) break;
      last = where[j];
      maxi(res, j - i + 1);
    }
  }
  cout << n - res << '\n';
}
int main() {
  ios_base::sync_with_stdio(0);
  int z;
  cin >> z;
  while (z--) solve();
}
