#include <bits/stdc++.h>
using namespace std;
int dp[300005], forward1[300005], back[300005], a[300005];
int main() {
  ios::sync_with_stdio(0);
  cin.tie(0);
  cout.tie(0);
  ;
  int q;
  cin >> q;
  memset(forward1, -1, sizeof(forward1));
  memset(back, -1, sizeof(back));
  while (q--) {
    int n;
    cin >> n;
    set<int> s;
    for (int i = 1; i <= n; i++) {
      cin >> a[i];
      s.insert(a[i]);
    }
    for (int i = 1; i <= n; i++)
      if (forward1[a[i]] == -1) forward1[a[i]] = i;
    for (int i = n; i >= 1; i--)
      if (back[a[i]] == -1) back[a[i]] = i;
    vector<int> vec;
    for (auto k : s) vec.push_back(k);
    int final = 1;
    dp[0] = 1;
    for (int i = 1; i < vec.size(); i++) {
      if (forward1[vec[i]] > back[vec[i - 1]]) {
        dp[i] = 1 + dp[i - 1];
        final = max(final, dp[i]);
      } else
        dp[i] = 1;
    }
    cout << s.size() - final << "\n";
    for (int i = 1; i <= n; i++) {
      forward1[a[i]] = back[a[i]] = -1;
      dp[i - 1] = 0;
    }
  }
}
