#include <bits/stdc++.h>
using namespace std;
int q, n;
int a[300005];
vector<int> pos[300005];
vector<int> v;
int dp[300005];
int main() {
  ios_base::sync_with_stdio(0);
  ;
  cin >> q;
  while (q--) {
    cin >> n;
    for (int i = 1; i <= n; i++) {
      pos[i].clear();
      dp[i] = 0;
    }
    v.clear();
    for (int i = 0; i < n; i++) {
      cin >> a[i];
      pos[a[i]].push_back(i + 1);
      v.emplace_back(a[i]);
    }
    sort(v.begin(), v.end());
    v.resize(unique(v.begin(), v.end()) - v.begin());
    int unq = (int)v.size();
    dp[0] = 1;
    int maxdp = 1;
    for (int i = 1; i < unq; i++) {
      int first = pos[v[i]].front();
      int last = pos[v[i - 1]].back();
      dp[i] = (first > last) ? dp[i - 1] + 1 : 1;
      maxdp = max(maxdp, dp[i]);
    }
    cout << unq - maxdp << "\n";
  }
  return 0;
}
