#include <bits/stdc++.h>
using namespace std;
int main() {
  int q;
  cin >> q;
  while (q--) {
    int n;
    cin >> n;
    vector<int> pos[n + 1];
    int a[n + 1];
    for (int i = 1; i <= n; i++) {
      cin >> a[i];
      pos[a[i]].push_back(i);
    }
    vector<int> pre;
    for (int i = 1; i <= n; i++) {
      if ((int)(pos[i].size()) > 0) {
        pre.push_back(i);
      }
    }
    int maxx = -6;
    int end = -8;
    int cnt = 0;
    for (int i = 0; i < pre.size(); i++) {
      if (pos[pre[i]].front() > end) {
        cnt++;
        end = pos[pre[i]].back();
        maxx = max(cnt, maxx);
      } else {
        cnt = 1;
        end = pos[pre[i]].back();
        maxx = max(maxx, cnt);
      }
    }
    cout << (int)(pre.size()) - maxx << endl;
  }
}
