#include <bits/stdc++.h>
using namespace std;
template <class T>
bool uin(T &a, T b) {
  return a > b ? (a = b, true) : false;
}
template <class T>
bool uax(T &a, T b) {
  return a < b ? (a = b, true) : false;
}
vector<long long> arr;
bool ok(vector<long long> a1, long long r, long long n, vector<long long> cpy) {
  long long ind = n - 1;
  long long cnt = 0;
  vector<long long> a2;
  while (cnt < r) {
    if (ind < a1.size()) break;
    a2.push_back(cpy[ind]);
    --ind;
    ++cnt;
  }
  if (a2.size() == 0) a2.push_back(1e10);
  if (a1.size() == 0) a1.push_back(-1);
  for (long long i = n - 1; i >= 0; --i) {
    if (arr[i] > a1.back() && arr[i] < a2.back()) a2.push_back(arr[i]);
  }
  reverse(a2.begin(), a2.end());
  if (a1.back() == -1) a1.pop_back();
  for (auto &i : a2)
    if (i != 1e10) a1.push_back(i);
  return cpy == (a1);
}
long long go(vector<long long> &arr) {
  long long n = arr.size();
  vector<long long> cpy(arr);
  sort(cpy.begin(), cpy.end());
  long long ans = n;
  vector<long long> a1;
  for (long long l = 0; l < n; ++l) {
    if (l > 0) a1.push_back(cpy[l - 1]);
    long long left = -1, right = n - left;
    while (left < right - 1) {
      long long md = (left + right) / 2;
      if (ok(a1, md, n, cpy))
        right = md;
      else
        left = md;
    }
    ans = min(l + right, ans);
  }
  return ans;
}
void solve() {
  long long n;
  cin >> n;
  arr.assign(n, 0);
  for (long long i = 0; i < n; ++i) cin >> arr[i];
  cout << go(arr) << '\n';
}
signed main() {
  cin.tie(NULL), ios_base::sync_with_stdio(false), cout.tie(NULL);
  long long t = 1;
  cin >> t;
  while (t--) solve();
  return 0;
}
