#include <bits/stdc++.h>
using namespace std;
const long long INF = 1e18;
const int inf = 1e9;
const long double eps = 1e-7;
const long double pi = acos(-1);
const int dx[8] = {0, 0, 1, -1, 1, 1, -1, -1};
const int dy[8] = {1, -1, 0, 0, 1, -1, 1, -1};
const int N = 3e5 + 11;
const int mod1 = 998244353;
const int mod2 = 1e9 + 7;
const int M = 4002;
int a[N], l[N], r[N];
int dp[N];
int main() {
  int t;
  cin >> t;
  while (t--) {
    int n;
    cin >> n;
    vector<int> v;
    for (int i = 1; i <= n; i++) {
      cin >> a[i];
      if (r[a[i]] == 0) {
        l[a[i]] = i;
        v.push_back(a[i]);
      }
      r[a[i]] = i;
    }
    sort(v.begin(), v.end());
    reverse(v.begin(), v.end());
    dp[0] = 1;
    int mx = 1;
    for (int i = 1; i < (int)v.size(); i++) {
      if (r[v[i]] < l[v[i - 1]])
        dp[i] = dp[i - 1] + 1;
      else
        dp[i] = 1;
      mx = max(mx, dp[i]);
    }
    cout << (int)v.size() - mx << "\n";
    for (int i = 0; i < (int)v.size(); i++) {
      l[v[i]] = 0;
      r[v[i]] = 0;
    }
  }
}
