#include <bits/stdc++.h>
using namespace std;
const int MX = 1000000;
const long long inf = 1e9;
const long long mod = 1e9 + 7;
int a[MX], b[MX];
map<int, int> dp, nxt, mp, mp2;
int main() {
  ios_base::sync_with_stdio(0);
  cin.tie(0);
  cout.tie(0);
  int tc;
  cin >> tc;
  while (tc--) {
    int n;
    cin >> n;
    dp.clear(), nxt.clear();
    for (int i = 1; i <= n; b[i] = a[i], i++) cin >> a[i];
    sort(b + 1, b + n + 1);
    nxt[b[n]] = inf;
    for (int i = n - 1; i >= 1; i--) nxt[b[i]] = b[i + 1];
    int mx = 0;
    for (int i = n; i >= 1; i--) {
      dp[a[i]] = dp[nxt[a[i]]] + 1;
      mx = max(mx, dp[a[i]]);
    }
    cout << n - mx << endl;
  }
  return 0;
}
