#include <bits/stdc++.h>
using namespace std;
long long gcd(long long x, long long y) {
  while (x > 0) {
    y %= x;
    swap(x, y);
  }
  return y;
}
int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(0);
  int q;
  cin >> q;
  while (q--) {
    int n;
    cin >> n;
    vector<bool> valid(n, false);
    vector<int> low(n, n), high(n, 0);
    int tmp;
    for (int i = 0; i < n; i++) {
      cin >> tmp;
      tmp--;
      valid[tmp] = true;
      low[tmp] = min(low[tmp], i);
      high[tmp] = i;
    }
    int i, j;
    vector<int> cnt(n);
    for (i = n - 1; i >= 0; i--)
      if (valid[i]) break;
    int cc = 1;
    int ni;
    for (ni = i - 1; i >= 0;) {
      for (ni = i - 1; ni >= 0; ni--)
        if (valid[ni]) break;
      if (ni >= 0) {
        if (high[ni] > low[i])
          cnt[ni] = cc;
        else
          cnt[ni] = cnt[i];
      }
      cc++;
      i = ni;
    }
    for (i = 0; i < n; i++)
      if (valid[i]) break;
    vector<int> cnt2(n);
    cc = 1;
    int res = cnt[i];
    for (; i < n;) {
      for (ni = i + 1; ni < n; ni++)
        if (valid[ni]) break;
      if (ni < n) {
        if (high[i] > low[ni])
          cnt2[ni] = cc;
        else
          cnt2[ni] = cnt2[i];
        cc++;
        res = min(res, cnt[i] + cnt2[i]);
      } else if (ni == n)
        res = min(res, cnt2[i]);
      i = ni;
    }
    cout << res << '\n';
  }
  return 0;
}
