#include <bits/stdc++.h>
using namespace std;
const int Sz = 3e5 + 5;
int a[Sz];
int f[Sz];
vector<int> zip;
int n;
int main() {
  ios_base::sync_with_stdio(0);
  cin.tie(0);
  cout.tie(0);
  int Test;
  cin >> Test;
  while (Test--) {
    cin >> n;
    for (int i = 1; i <= n; i++) {
      cin >> a[i];
      zip.push_back(a[i]);
    }
    sort(zip.begin(), zip.end());
    zip.resize(unique(zip.begin(), zip.end()) - zip.begin());
    for (int i = 1; i <= n; i++) {
      a[i] = upper_bound(zip.begin(), zip.end(), a[i]) - zip.begin();
    }
    int res = 1;
    memset(f, 0, sizeof f);
    f[0] = 0;
    for (int i = 1; i <= n; i++) {
      if (f[a[i]] != 0)
        f[a[i]] = max(f[a[i]] + 1, f[a[i] - 1] + 1);
      else
        f[a[i]] = f[a[i] - 1] + 1;
      res = max(res, f[a[i]]);
    }
    cout << n - res << "\n";
    zip.clear();
  }
  return 0;
}
