#include <bits/stdc++.h>
using namespace std;
const long long inf = (long long)1e18;
const long long mod = (long long)1e9 + 7;
const double eps = (double)1e-9;
const double pi = acos(-1.0);
const int dx[] = {0, 0, 1, 0, -1};
const int dy[] = {0, 1, 0, -1, 0};
const int N = 300500;
int n, l[N], r[N], a[N], ans, c;
int main() {
  cin.tie(0);
  cout.tie(0);
  ios_base::sync_with_stdio(NULL);
  int T;
  cin >> T;
  while (T--) {
    cin >> n;
    for (int i = 1; i <= n; ++i) l[i] = 0, r[i] = n;
    for (int i = 1; i <= n; ++i) {
      cin >> a[i];
      if (l[a[i]] == 0) l[a[i]] = i;
      r[a[i]] = i;
    }
    sort(a + 1, a + n + 1);
    ans = 1, c = 0;
    int m = 1;
    for (int i = 2; i <= n; ++i) {
      if (a[i] == a[i - 1]) continue;
      m++;
      if (r[a[i - 1]] < l[a[i]])
        c++;
      else
        c = 0;
      ans = max(ans, c + 1);
    }
    cout << m - ans << endl;
  }
  return 0;
}
