#include <bits/stdc++.h>
using namespace std;
const long long LINF = (long long)1e18 + 47;
const int INF = 2 * 1e9 + 47;
const int MOD = 1e9 + 7;
const int modulo = 1e8;
const int nax = 3 * (int)1e3 + 10;
const double EPS = 1e-7;
int solve(vector<int> a, int n) {
  vector<int> dp(n, 0);
  dp[0] = 1;
  for (int i = (1); i < (n); i++) {
    dp[i] = 1;
    for (int j = (0); j < (i); j++) {
      if (a[j] + 1 == a[i] || a[j] == a[i]) {
        dp[i] = max(dp[i], dp[j] + 1);
      }
    }
  }
  return n - *max_element(dp.begin(), dp.end());
}
int main() {
  ios_base::sync_with_stdio(0), cin.tie(0), cout.tie(0);
  int tt;
  cin >> tt;
  for (int test = (1); test < (tt + 1); test++) {
    int n;
    cin >> n;
    vector<pair<int, int>> a(n);
    for (int i = (0); i < (n); i++) {
      cin >> a[i].first;
      a[i].second = i;
    }
    vector<int> b(n);
    sort(a.begin(), a.end());
    int z = 0;
    for (int i = (0); i < (n); i++) {
      b[a[i].second] = z;
      if (i == n - 1) continue;
      if (a[i].first != a[i + 1].first) z++;
    }
    cout << solve(b, n) << '\n';
  }
}
