#include <bits/stdc++.h>
#pragma GCC optimize("O3")
#pragma comment(linker, "/stack:200000000")
#pragma GCC optimize("unroll-loops")
using namespace std;
const long long mod = 998244353;
const long long sz = 2e5 + 9;
long long gcd(long long a, long long b) {
  if (b == 0) return a;
  return gcd(b, a % b);
}
long long BL[200002];
struct query {
  long long l, r, idx;
  bool operator<(const query &rhs) {
    return (BL[l] == BL[rhs.l]) ? (r < rhs.r) : (BL[l] < BL[rhs.l]);
  }
};
long long power(long long x, long long y, long long p) {
  long long res = 1;
  x = x % p;
  while (y > 0) {
    if (y & 1) {
      res = (res * x) % p;
    }
    y = y >> 1;
    x = (x * x) % p;
  }
  return res;
}
void ruk_teri_maa_chodtha_hu() {
  int n, m;
  cin >> n;
  vector<pair<long long, long long> > v(n);
  for (long long i = 0; i < n; i++) {
    cin >> m;
    v[i] = {m, i};
  }
  long long prev = v[0].first, f = 0;
  for (long long i = 1; i < n; i++) {
    if (prev > v[i].first) {
      f = 1;
      break;
    }
    prev = v[i].first;
  }
  if (f == 0) {
    cout << 0 << '\n';
    return;
  }
  sort(v.begin(), v.end());
  long long ans = 0, c = 1;
  for (long long i = 1; i < n; i++) {
    if (v[i - 1].first <= v[i].first && v[i - 1].second < v[i].second)
      c++;
    else
      c = 1;
    ans = max(c, ans);
  }
  cout << n - ans << '\n';
}
void solve() {
  long long tc;
  cin >> tc;
  while (tc--) ruk_teri_maa_chodtha_hu();
}
int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
  cout.tie(NULL);
  clock_t beg = clock();
  solve();
  clock_t end = clock();
  fprintf(stderr, "%.3f sec, Copyright %c 2019 PyThor. \n",
          double(end - beg) / CLOCKS_PER_SEC, 184);
  return 0;
}
