#include <bits/stdc++.h>
using namespace std;
int const N = 3e5 + 5;
int const MAX = 1e9 + 7;
int n, a[N];
int mins[N], maxs[N];
bool cmp(pair<int, int> p, pair<int, int> q) { return p.second < q.second; }
struct BIT {
  int tree[N];
  void clear() {
    for (int i = 1; i <= n; i++) {
      tree[i] = 0;
    }
  }
  void update(int x, int val) {
    while (x <= n) {
      tree[x] = max(tree[x], val);
      x += x & (-x);
    }
  }
  int get(int x) {
    int res = 0;
    while (x) {
      res = max(res, tree[x]);
      x -= x & (-x);
    }
    return res;
  }
};
BIT t;
int dp[N];
int main() {
  ios_base::sync_with_stdio(0);
  int q;
  cin >> q;
  while (q--) {
    cin >> n;
    for (int i = 1; i <= n; i++) {
      mins[i] = MAX;
      maxs[i] = -MAX;
    }
    for (int i = 1; i <= n; i++) {
      cin >> a[i];
      mins[a[i]] = min(mins[a[i]], i);
      maxs[a[i]] = max(maxs[a[i]], i);
    }
    vector<pair<int, int> > val;
    for (int i = 1; i <= n; i++) {
      if (mins[i] != MAX) {
        val.push_back(make_pair(mins[i], maxs[i]));
      }
    }
    int ans = 0;
    for (int i = 0; i < val.size();) {
      int pos = i + 1;
      while (pos < val.size() && val[pos].first > val[pos - 1].second) {
        pos++;
      }
      ans = max(ans, pos - i);
      i = pos;
    }
    cout << (int)val.size() - ans << endl;
    t.clear();
  }
}
