#include <bits/stdc++.h>
#pragma GCC optimize("O2")
#pragma GCC optimize("tree-vectorize")
#pragma GCC target("sse4")
using namespace std;
using lint = long long;
using pii = pair<int, int>;
using pll = pair<lint, lint>;
template <typename T>
using vc = vector<T>;
template <typename T>
using vvc = vector<vector<T>>;
template <typename T>
inline bool chmax(T& a, T b) {
  if (a < b) {
    a = b;
    return true;
  }
  return false;
}
template <typename T>
inline bool chmin(T& a, T b) {
  if (a > b) {
    a = b;
    return true;
  }
  return false;
}
constexpr lint ten(int n) { return n == 0 ? 1 : ten(n - 1) * 10; }
class F1FlyingSortEasyVersion {
 public:
  void solve(std::istream& in, std::ostream& out) {
    ios_base::sync_with_stdio(false);
    in.tie(nullptr), out.tie(nullptr);
    int tt;
    in >> tt;
    while (tt--) {
      int N;
      in >> N;
      map<int, int> id;
      for (int i = (0); i < (N); ++i) {
        int a;
        in >> a;
        id[a] = i;
      }
      int mx = 0, len = 0, last = -1;
      for (auto& p : id) {
        if (last < p.second)
          ++len;
        else
          len = 1;
        chmax(mx, len);
        last = p.second;
      }
      out << N - mx << '\n';
    }
  }
};
int main() {
  F1FlyingSortEasyVersion solver;
  std::istream& in(std::cin);
  std::ostream& out(std::cout);
  solver.solve(in, out);
  return 0;
}
