#include <bits/stdc++.h>
using namespace std;
const long long mod = 1e9 + 7;
long long dx[] = {-1, 0, 1, 0};
long long dy[] = {0, -1, 0, 1};
int32_t main() {
  ios::sync_with_stdio(false);
  cin.tie(0);
  cout.tie(0);
  long long t;
  cin >> t;
  while (t--) {
    long long n;
    cin >> n;
    vector<long long> v(n), x(n);
    for (long long i = 0; i < n; i++) {
      cin >> v[i];
      x[i] = v[i];
    }
    sort(x.begin(), x.end());
    map<long long, long long> m;
    long long cnt = 0;
    for (long long i = 0; i < n; i++) {
      if (m.find(x[i]) == m.end()) {
        m[x[i]] = cnt++;
      }
    }
    vector<pair<long long, long long> > p(m.size(), {-1, -1});
    for (long long i = 0; i < n; i++) {
      long long k = m[v[i]];
      if (p[k].first == -1) p[k].first = i;
      p[k].second = i;
    }
    long long res = 1;
    long long mx = 1;
    for (long long i = 1; i < p.size(); i++) {
      if (p[i].first > p[i - 1].second)
        mx++;
      else
        mx = 1;
      res = max(mx, res);
    }
    cout << p.size() - res << "\n";
  }
}
