#include <bits/stdc++.h>
using namespace std;
const int N = 2e5 + 10;
const int mod = 1e9 + 7;
int t, n, a[N], b[N], dp[N], vis[N], w[N];
int main() {
  ios::sync_with_stdio(false);
  cin >> t;
  while (t--) {
    cin >> n;
    for (int i = 1; i <= n; i++) {
      cin >> a[i];
      dp[i] = 1;
      b[i] = a[i];
      vis[i] = 0;
      w[i] = 0;
    }
    sort(b + 1, b + n + 1);
    int len = unique(b + 1, b + n + 1) - b - 1;
    for (int i = 1; i <= n; i++) {
      a[i] = lower_bound(b + 1, b + 1 + len, a[i]) - b;
    }
    for (int i = 1; i <= n; i++) {
      if (vis[a[i]]) {
        dp[i] = dp[vis[a[i]]] + 1;
      } else if (vis[a[i] - 1]) {
        dp[i] = dp[vis[a[i] - 1]] + 1;
      }
      vis[a[i]] = i;
    }
    int l = 0;
    for (int i = 1; i <= n; i++) {
      if (l < dp[i]) {
        l = dp[i];
      }
    }
    cout << n - l << "\n";
  }
  return 0;
}
