#include <bits/stdc++.h>
using namespace std;
signed main() {
  long long q;
  cin >> q;
  for (long long i = 0; i < q; i++) {
    long long n;
    cin >> n;
    vector<long long> v(n);
    vector<long long> g(n, -1), p(n, -1);
    for (long long j = 0; j < n; j++) {
      cin >> v[j];
      if (g[v[j] - 1] == -1) {
        g[v[j] - 1] = j;
      }
      p[v[j] - 1] = j;
    }
    vector<pair<long long, long long> > gen;
    for (long long i = 0; i < n; i++) {
      if (g[i] != -1) {
        gen.push_back({g[i], p[i]});
      }
    }
    if (gen.size() == 0) {
      cout << 0;
    } else {
      vector<long long> left_cnt(gen.size()), right_cnt(gen.size());
      for (long long i = 0; i < gen.size() - 1; i++) {
        if (gen[i].second > gen[i + 1].first) {
          left_cnt[i + 1] = i + 1;
        } else {
          left_cnt[i + 1] = left_cnt[i];
        }
      }
      for (long long i = gen.size() - 1; i > 0; i--) {
        if (gen[i].first < gen[i - 1].second) {
          right_cnt[i - 1] = gen.size() - i;
        } else {
          right_cnt[i - 1] = right_cnt[i];
        }
      }
      long long min = 1e9;
      for (long long i = 0; i < gen.size(); i++) {
        if (left_cnt[i] + right_cnt[i] < min) {
          min = left_cnt[i] + right_cnt[i];
        }
      }
      cout << min;
    }
    cout << endl;
  }
  return 0;
}
