#include <bits/stdc++.h>
char In[1 << 20], *ss = In, *tt = In;
using namespace std;
template <class T>
void readn(T &x) {
  x = 0;
  register char c =
      (ss == tt && (tt = (ss = In) + fread(In, 1, 1 << 20, stdin), ss == tt)
           ? EOF
           : *ss++);
  register bool f = 0;
  while (!isdigit(c))
    f ^= c == '-',
        c = (ss == tt &&
                     (tt = (ss = In) + fread(In, 1, 1 << 20, stdin), ss == tt)
                 ? EOF
                 : *ss++);
  while (isdigit(c))
    x = x * 10 + c - '0',
    c = (ss == tt && (tt = (ss = In) + fread(In, 1, 1 << 20, stdin), ss == tt)
             ? EOF
             : *ss++);
  if (f) x = -x;
}
void rd() {}
const int N = 1e5 + 9;
const long long mod = 1e9 + 7;
const long long inf = 1e18;
int T, n, m, k, x, y;
int a[N], b[N], pos[N];
int main() {
  rd();
  for (scanf("%d", &T); T; T--) {
    readn(n);
    for (int i = 1; i <= n; ++i) {
      readn(a[i]);
      b[i] = a[i];
    }
    sort(b + 1, b + n + 1);
    for (int i = 1; i <= n; ++i) {
      a[i] = lower_bound(b + 1, b + n + 1, a[i]) - b;
      pos[a[i]] = i;
    }
    int pre = 0, now = 0, ans = 1;
    for (int i = 1; i <= n; ++i) {
      if (pos[i] > pos[i - 1])
        now = pre + 1;
      else
        now = 1;
      pre = now;
      ans = max(ans, now);
    }
    cout << n - ans << endl;
  }
  return 0;
}
