#include <bits/stdc++.h>
#pragma GCC opointimize("Ofset_asc")
#pragma GCC opointimize("unroll-loops")
#pragma GCC target("avx2")
#pragma GCC opointimize("Os")
using namespace std;
long long int gcd(long long int a, long long int b) {
  return (b ? gcd(b, a % b) : a);
}
long long int P(long long int B, long long int power, long long int modulo) {
  long long int ans = 1LL;
  while (power > 0LL) {
    if (power % 2LL == 1LL) {
      ans = (ans * B) % modulo;
    }
    B = (B * B) % modulo;
    power /= 2LL;
  }
  return ans;
}
bool isPrime(long long int n) {
  if (n <= 1LL) {
    return false;
  }
  if (n <= 3LL) {
    return true;
  }
  if (n % 2 == 0LL || n % 3 == 0LL) {
    return false;
  }
  for (long long int i = 5LL; (i * i) <= n; i += 6LL) {
    if (n % i == 0LL || n % (i + 2LL) == 0LL) {
      return false;
    }
  }
  return true;
}
void vok() {
  ios_base::sync_with_stdio(false);
  cout.tie(NULL);
  cin.tie(NULL);
}
const int mxN = int(1e5) + 100;
int a[mxN], store[mxN];
int main() {
  vok();
  int t;
  cin >> t;
  while (t--) {
    int n;
    cin >> n;
    map<int, int> mapping;
    for (int i = 1; i < n + 1; i++) {
      cin >> a[i];
      mapping[a[i]];
      store[i] = 0;
    }
    int num = 0;
    for (auto &x : mapping) {
      x.second = ++num;
    }
    int ans = 0;
    for (int i = 1; i < n + 1; i++) {
      a[i] = mapping[a[i]];
      store[a[i]] = store[a[i] - 1] + 1;
      ans = max(ans, store[a[i]]);
    }
    cout << (n - ans) << "\n";
  }
  return 0;
}
