#include <bits/stdc++.h>
using iii = int;
using yeee = iii;
const iii DEBUG = -1;
using namespace std;
using namespace chrono;
template <typename T1>
void DOUTf(ostringstream &os, int names_i, vector<string> &names, T1 &&val) {
  os << names[names_i] << " = " << val;
}
template <typename T1, typename... Ts>
void DOUTf(ostringstream &os, int names_i, vector<string> &names, T1 &&val,
           Ts &&...vals) {
  os << names[names_i] << " = " << val << " | ";
  DOUTf(os, names_i + 1, names, vals...);
}
template <typename... Ts>
string DOUTg(string names, Ts &&...vals) {
  string special = "()[]{}<>'\"\\";
  vector<string> vnames;
  vnames.push_back("");
  vector<char> stak;
  for (char c : names) {
    bool inside_string =
        !stak.empty() && (stak.back() == '\'' || stak.back() == '\"');
    if (c == '\n') c = ' ';
    if (c == ' ' && (vnames.back().empty() ||
                     (!inside_string && vnames.back().back() == ' ')))
      continue;
    if (stak.empty() && c == ',') {
      if (!vnames.back().empty() && vnames.back().back() == ' ')
        vnames.back().pop_back();
      vnames.push_back("");
      continue;
    }
    vnames.back().push_back(c);
    if (!stak.empty() && stak.back() == '\\') {
      stak.pop_back();
      continue;
    }
    size_t p = special.find(c);
    bool closing = p < 8 && p % 2;
    if (p == string::npos) continue;
    if (p == special.size() - 1) {
      stak.push_back(c);
      continue;
    }
    if (stak.empty()) {
      if (!closing) stak.push_back(c);
      continue;
    }
    if (inside_string) {
      if (c == stak.back()) stak.pop_back();
      continue;
    }
    if (closing) {
      if (stak.back() == special[p - 1]) stak.pop_back();
      continue;
    }
    stak.push_back(c);
  }
  ostringstream os;
  DOUTf(os, 0, vnames, vals...);
  return os.str();
}
template <template <typename...> class Container, typename T, size_t DIMENSION>
struct MultiDimensional {
  using internal = MultiDimensional<Container, T, DIMENSION - 1>;
  using type = Container<typename internal::type>;
  template <typename... Args>
  static type Generate(T def, const size_t size, Args... sizes) {
    return type(size, internal::Generate(def, sizes...));
  }
};
template <template <typename...> class Container, typename T>
struct MultiDimensional<Container, T, 1> {
  using internal = T;
  using type = Container<T>;
  static type Generate(T def, const size_t size) { return type(size, def); }
};
template <typename T, typename... Args>
auto make_vector(T def, Args... sizes) ->
    typename MultiDimensional<std::vector, T, sizeof...(sizes)>::type {
  return MultiDimensional<std::vector, T, sizeof...(sizes)>::Generate(def,
                                                                      sizes...);
}
template <typename T>
T get() {
  T x;
  cin >> x;
  return x;
}
template <typename T1, typename T2>
ostream &operator<<(ostream &out, const pair<T1, T2> &cont) {
  out << "(" << cont.first << ", " << cont.second << ")";
  return out;
}
template <typename T, typename Alloc>
ostream &operator<<(ostream &out, const vector<T, Alloc> &cont) {
  for (auto it = cont.begin(); it != cont.end(); ++it)
    out << (it == cont.begin() ? "" : " ") << *it;
  return out;
}
template <typename T, typename Alloc>
ostream &operator<<(ostream &out, const deque<T, Alloc> &cont) {
  for (auto it = cont.begin(); it != cont.end(); ++it)
    out << (it == cont.begin() ? "" : " ") << *it;
  return out;
}
template <typename T, typename Compare, typename Alloc>
ostream &operator<<(ostream &out, const set<T, Compare, Alloc> &cont) {
  out << "{";
  for (auto it = cont.begin(); it != cont.end(); ++it)
    out << (it == cont.begin() ? "" : ", ") << *it;
  out << "}";
  return out;
}
template <typename T, typename Compare, typename Alloc>
ostream &operator<<(ostream &out, const multiset<T, Compare, Alloc> &cont) {
  out << "{";
  for (auto it = cont.begin(); it != cont.end(); ++it)
    out << (it == cont.begin() ? "" : ", ") << *it;
  out << "}";
  return out;
}
template <typename Key, typename T, typename Compare, typename Alloc>
ostream &operator<<(ostream &out, const map<Key, T, Compare, Alloc> &cont) {
  out << "{";
  for (auto it = cont.begin(); it != cont.end(); ++it)
    out << (it == cont.begin() ? "" : ", ") << it->first << ":" << it->second;
  out << "}";
  return out;
}
template <typename Key, typename T, typename Compare, typename Alloc>
ostream &operator<<(ostream &out,
                    const multimap<Key, T, Compare, Alloc> &cont) {
  out << "{";
  for (auto it = cont.begin(); it != cont.end(); ++it)
    out << (it == cont.begin() ? "" : ", ") << it->first << ":" << it->second;
  out << "}";
  return out;
}
template <typename T>
ostream &operator<<(ostream &out, vector<vector<T>> cont) {
  for (auto it = cont.begin(); it != cont.end(); ++it) out << *it << "\n";
  return out;
}
using ll = long long;
using lll = long long;
using ull = unsigned long long;
using ld = long double;
using pll = pair<ll, ll>;
using vb = vector<bool>;
using vvb = vector<vb>;
using vll = vector<ll>;
using vvll = vector<vll>;
using vpll = vector<pll>;
using point = complex<ld>;
ld get_time() {
  return ld(duration_cast<nanoseconds>(
                high_resolution_clock::now().time_since_epoch())
                .count()) /
         1e9;
}
ll sign(ld x) { return (x > 0) - (x < 0); }
ll fmod(ll x, ll mod) {
  if (!mod) return x;
  if (abs(x) >= mod) x %= mod;
  if (x < 0) x += mod;
  return x;
}
ll pow(ll x, ll exp, ll mod) {
  ll res = 1, y = x;
  while (exp) {
    if (!mod) {
      if (exp & 1) res = res * y;
      y = y * y;
    } else {
      if (exp & 1) res = fmod(res * y, mod);
      y = fmod(y * y, mod);
    }
    exp >>= 1;
  }
  return res;
}
ld START_TIME = get_time();
mt19937_64 rand_mt64(chrono::steady_clock::now().time_since_epoch().count());
mt19937 rand_mt(chrono::steady_clock::now().time_since_epoch().count());
const long long INFll = ll(1.1e18) - 1;
const int INFi = ll(1.1e9) - 1;
ll INF = sizeof(ll) == sizeof(int) ? ll(INFi) : ll(INFll);
char newl = '\n';
void main_init(string input_file = "", string output_file = "",
               bool fast_cio = true) {
  srand(unsigned(time(0)));
  if (fast_cio) {
    ios::sync_with_stdio(false);
    cin.tie(0);
    cout.tie(0);
  }
  if (DEBUG >= 0 && input_file != "") freopen(input_file.c_str(), "r", stdin);
  if (DEBUG >= 0 && output_file != "")
    freopen(output_file.c_str(), "w", stdout);
}
yeee main() {
  main_init();
  ll TC;
  cin >> TC;
  while (TC--) {
    ll N;
    cin >> N;
    vll V(N);
    for (ll i = ll(0); i < ll(N); ++i) cin >> V[i];
    vll V2 = V;
    sort((V2).begin(), (V2).end());
    V2.resize(unique((V2).begin(), (V2).end()) - V2.begin());
    ll M = ll((V2).size());
    map<ll, ll> Minv;
    for (ll i = ll(0); i < ll(ll((V2).size())); ++i) Minv[V2[i]] = i;
    for (ll i = ll(0); i < ll(N); ++i) V[i] = Minv[V[i]];
    if (DEBUG >= 1) cout << V << endl;
    vb B(M + 1, false);
    vll C(M, true);
    C[M - 1] = false;
    for (ll i = ll(0); i < ll(N); ++i) {
      if (B[V[i] + 1]) C[V[i]] = false;
      B[V[i]] = true;
    }
    ll res = INF;
    ll r = 0;
    for (ll i = ll(0); i < ll(M); ++i) {
      r++;
      if (!C[i]) r = 0;
      res = min(res, M - r - 1);
    }
    cout << res << endl;
  }
}
