#include <bits/stdc++.h>
using namespace std;
int a[300005];
int l[300005];
int f[300005];
int ans[300005];
int main() {
  int q;
  cin >> q;
  while (q--) {
    int n;
    cin >> n;
    int i;
    for (i = 1; i <= n; i++) f[i] = -1, l[i] = -1, ans[i] = -1;
    int s = 0;
    for (i = 1; i <= n; i++) {
      cin >> a[i], l[a[i]] = i;
      if (f[a[i]] == -1) f[a[i]] = i, s++;
    }
    int p = -1;
    for (i = 1; i <= n; i++) {
      if (f[i] != -1) {
        if (p != -1) {
          if (l[p] < f[i])
            ans[i] = ans[p] + 1;
          else
            ans[i] = 0;
        } else
          ans[i] = 0;
        p = i;
      }
    }
    int c = 0;
    for (i = 1; i <= n; i++) c = max(c, ans[i]);
    cout << s - c - 1 << "\n";
  }
  return 0;
}
