#include <bits/stdc++.h>
const long long lim = 1e5 + 5;
using namespace std;
vector<long long> v;
bool vis[lim], vis1[lim];
long long n, m, k;
vector<long long> adj[100005];
set<long long> s;
long long ans, cnt;
long long a[500005];
long long power(long long a, long long b) {
  long long res = 1;
  while (b > 0) {
    if (b & 1) res = (res * a) % 1000000007;
    a = (a * a) % 1000000007;
    b >>= 1;
  }
  return res;
}
void dfs(long long cur, long long cnt, long long str, long long sum) {}
signed main() {
  ios_base::sync_with_stdio(false);
  cin.tie(0);
  cout.tie(0);
  ;
  long long q;
  cin >> q;
  map<long long, long long> mp;
  while (q--) {
    cin >> n;
    long long a[n + 5];
    pair<long long, long long> b[n + 5];
    ;
    for (long long i = 1; i < n + 1; i++) b[i] = {1e18, 0};
    ;
    for (long long i = 1; i < n + 1; i++) {
      cin >> a[i];
      b[a[i]].first = min(b[a[i]].first, i);
      b[a[i]].second = max(b[a[i]].second, i);
    }
    long long x = 0, val = 1, cnt = 0, ans = 0;
    ;
    for (long long i = 1; i < n + 1; i++) {
      if (b[i].second == 0) continue;
      cnt++;
      if (x == 0) {
        x = i;
        continue;
      } else if (b[i].first > b[x].second)
        val++;
      else {
        ans = max(val, ans);
        val = 1;
      }
      x = i;
    }
    ans = max(ans, val);
    cout << cnt - ans << "\n";
  }
}
