#include <bits/stdc++.h>
#pragma GCC optimize("Ofast")
#pragma GCC optimization("unroll-loops")
const int N = 3e5 + 5;
const long long int mod = 1e9 + 7;
const long long int Mod = 998244353;
const long double Pi = acos(-1);
const long long int Inf = 4e18;
int dx[9] = {0, 1, -1, 0, 0, 1, 1, -1, -1};
int dy[9] = {0, 0, 0, 1, -1, 1, -1, 1, -1};
using namespace std;
void TestCase() {
  int n, ans = N, tot = 0;
  cin >> n;
  vector<pair<int, int> > g(n + 1, {n + 1, 0});
  for (int i = 1, x; i <= n; i++) {
    cin >> x;
    g[x].first = min(g[x].first, i);
    g[x].second = max(g[x].second, i);
  }
  for (int i = 1; i <= n; i++) {
    if (g[i].second) tot++;
  }
  for (int i = 1; i <= n; i++) {
    int cnt = 0, j = i, r = 0;
    while (j <= n && g[j].first > r) {
      if (g[j].second) {
        cnt++;
        r = g[j].second;
      }
      j++;
    }
    ans = min(ans, tot - cnt);
    i = j - 1;
  }
  cout << ans;
}
int main() {
  ios_base::sync_with_stdio(0);
  cin.tie(0);
  ;
  int T = 1;
  cin >> T;
  while (T--) {
    TestCase();
    cout << "\n";
  }
  return 0;
}
