#include <bits/stdc++.h>
#pragma GCC optimize("Ofast")
using namespace std;
int main() {
  cin.tie(nullptr)->sync_with_stdio(false);
  int q;
  cin >> q;
  while (q--) {
    int n;
    cin >> n;
    vector<int> a(n);
    for (auto &i : a) {
      cin >> i;
    }
    vector<int> max_ind(n + 1, -1), min_ind(n + 1, -1);
    for (int i = 0; i < n; ++i) {
      max_ind[a[i]] = i;
    }
    for (int i = 0; i < n; ++i) {
      if (min_ind[a[i]] == -1) {
        min_ind[a[i]] = i;
      }
    }
    vector<int> s = a;
    sort(s.begin(), s.end());
    s.erase(unique(s.begin(), s.end()), s.end());
    int m = s.size();
    vector<int> dp(m, 0);
    dp[0] = 1;
    for (int i = 1; i < m; ++i) {
      if (max_ind[s[i - 1]] < min_ind[s[i]]) {
        dp[i] = dp[i - 1] + 1;
      } else {
        dp[i] = 1;
      }
    }
    cout << m - *max_element(dp.begin(), dp.end()) << '\n';
  }
}
