#include <bits/stdc++.h>
using namespace std;
void dbg_out() { cerr << "\b\b]\n"; }
template <typename Head, typename... Tail>
void dbg_out(Head H, Tail... T) {
  cerr << H << ", ";
  dbg_out(T...);
}
void solve() {
  int n;
  cin >> n;
  map<int, pair<int, int>> mp;
  vector<int> A(n), nums;
  for (int i = 0; i < n; ++i) {
    int a;
    cin >> a;
    A[i] = a;
    if (mp.count(a))
      mp[a].second = i;
    else
      mp[a] = {i, i};
  }
  vector<array<int, 3>> intervals;
  for (auto i : mp) {
    nums.push_back(i.first);
    intervals.push_back({i.second.first, i.second.second, i.first});
  }
  sort(intervals.begin(), intervals.end());
  int len = 1;
  set<int> exists;
  set<array<int, 4>> dp;
  for (int i = 0; i < (int)intervals.size(); ++i) {
    int idx =
        lower_bound(nums.begin(), nums.end(), intervals[i][2]) - nums.begin();
    int curr = 1;
    if (idx and i and exists.find(nums[idx - 1]) != exists.end()) {
      idx--;
      auto it = *dp.lower_bound({nums[idx], -1, -1, -1});
      if (it[2] < intervals[i][0]) {
        curr = it[3] + 1;
      }
    }
    len = max(len, curr);
    dp.insert({intervals[i][2], intervals[i][0], intervals[i][1], curr});
    exists.insert(intervals[i][2]);
  }
  cout << (int)intervals.size() - len << '\n';
}
int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(nullptr);
  int t;
  cin >> t;
  while (t--) solve();
  return 0;
}
