#include <bits/stdc++.h>
using namespace std;
const int INF = 1e9;
int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(0), cout.tie(0);
  int q;
  scanf("%d", &q);
  while (q--) {
    int n;
    scanf("%d", &n);
    vector<int> l(n, INF), r(n, -INF), dp(n, 0);
    vector<int> v;
    for (int i = 0; i < n; i++) {
      int x;
      scanf("%d", &x);
      --x;
      v.push_back(x);
      l[x] = min(l[x], i);
      r[x] = max(r[x], i);
    }
    sort(v.begin(), v.end());
    v.erase(unique(v.begin(), v.end()), v.end());
    int res = n;
    for (int i = (int)v.size() - 1; i >= 0; i--) {
      if (i + 1 == (int)v.size() || r[v[i]] >= l[v[i + 1]]) {
        dp[i] = 1;
      } else {
        dp[i] = dp[i + 1] + 1;
      }
      res = min(res, (int)v.size() - dp[i]);
    }
    printf("%d\n", res);
  }
  return 0;
}
