#include <bits/stdc++.h>
using namespace std;
int Q;
int N;
int A[303030];
int L[303030];
int R[303030];
int C[303030];
void solve() {
  int i, j, k, l, r, x, y;
  string s;
  cin >> Q;
  while (Q--) {
    cin >> N;
    vector<int> V;
    for (i = 0; i < (N); i++) {
      cin >> A[i];
      V.push_back(A[i]);
    }
    sort((V.begin()), (V.end()));
    V.erase(unique((V.begin()), (V.end())), V.end());
    for (i = 0; i < (V.size()); i++) {
      R[i] = C[i] = 0;
      L[i] = 1 << 30;
    }
    for (i = 0; i < (N); i++) {
      A[i] = lower_bound((V.begin()), (V.end()), A[i]) - V.begin();
      R[A[i]] = i;
      L[A[i]] = min(L[A[i]], i);
    }
    int ma = 0;
    for (i = 0; i < (N); i++)
      if (L[A[i]] == i) {
        if (A[i] == 0)
          C[A[i]] = 0;
        else {
          if (R[A[i] - 1] < i)
            C[A[i]] = C[A[i] - 1] + 1;
          else
            C[A[i]] = 0;
        }
        ma = max(ma, C[A[i]]);
      }
    cout << V.size() - (ma + 1) << endl;
  }
}
int main(int argc, char** argv) {
  string s;
  int i;
  if (argc == 1) ios::sync_with_stdio(false), cin.tie(0);
  for (i = 0; i < (argc - 1); i++) s += argv[i + 1], s += '\n';
  for (i = 0; i < (s.size()); i++) ungetc(s[s.size() - 1 - i], stdin);
  cout.tie(0);
  solve();
  return 0;
}
