#include <bits/stdc++.h>
using namespace std;
int vis[3004];
int dp[3004];
int n;
int rec(int x) {
  if (dp[x] != -1) return dp[x];
  if (vis[x + 1] > vis[x])
    return dp[x] = rec(x + 1);
  else
    return dp[x] = x;
}
int main() {
  ios::sync_with_stdio(0), cin.tie(0), cout.tie(0);
  int t;
  cin >> t;
  while (t--) {
    cin >> n;
    int i;
    int a[n];
    vector<int> op;
    for (i = 0; i < n; i++) cin >> a[i], op.push_back(a[i]);
    int t = 1;
    sort(op.begin(), op.end());
    map<int, int> tt;
    for (i = 0; i < n; i++) tt[op[i]] = t++;
    for (i = 0; i < n; i++) a[i] = tt[a[i]];
    for (i = 0; i <= n + 1; i++) vis[i] = -1, dp[i] = -1;
    for (i = 0; i < n; i++) vis[a[i]] = i;
    for (i = 1; i <= n; i++) rec(i);
    int ans = 1e9;
    int j;
    for (i = 0; i <= n; i++) {
      for (j = n + 1; j > i; j--) {
        int fir = i + 1;
        int sic = j - 1;
        if (dp[fir] >= dp[sic]) ans = min(ans, i + n + 1 - j);
      }
    }
    cout << ans << endl;
  }
  return 0;
}
