#include <bits/stdc++.h>
using namespace std;
int lis(vector<int> S) {
  vector<int> prev((int)(S).size());
  vector<pair<int, int> > res;
  for (int i = 0; i < ((int)(S).size()); ++i) {
    pair<int, int> el{S[i], i};
    auto it = lower_bound(res.begin(), res.end(), pair<int, int>{S[i], 0});
    if (it == res.end()) res.push_back(el), it = --res.end();
    *it = el;
    prev[i] = it == res.begin() ? 0 : (it - 1)->second;
  }
  int L = (int)(res).size(), cur = res.back().second;
  vector<int> ans(L);
  while (L--) ans[L] = cur, cur = prev[cur];
  return (int)(ans).size();
}
int main() {
  cin.sync_with_stdio(0);
  cin.tie(0);
  cin.exceptions(cin.failbit);
  int q;
  cin >> q;
  while (q--) {
    int n;
    cin >> n;
    vector<int> a(n), aa(n);
    for (int i = 0; i < (n); ++i) cin >> a[i];
    for (int i = 0; i < (n); ++i) aa[i] = a[i];
    sort(a.begin(), a.end());
    a.erase(unique(a.begin(), a.end()), a.end());
    int m = (int)(a).size();
    vector<int> b(n + 1);
    for (int i = 0; i < (m); ++i) b[a[i]] = i;
    for (int i = 0; i < (n); ++i) aa[i] = b[aa[i]];
    vector<int> c(m), d(m);
    for (int i = 0; i < (n); ++i) c[aa[i]] = i;
    for (int i = n - 1; i >= (0); --i) d[aa[i]] = i;
    int ans = 1, mans = 1;
    for (int i = 1; i <= (m - 1); ++i) {
      if (d[i] > c[i - 1])
        ans++;
      else
        ans = 1;
      mans = max(ans, mans);
    }
    cout << m - mans << endl;
  }
  return 0;
}
