#include <bits/stdc++.h>
using namespace std;
namespace FGF {
int n, m;
const int N = 5e5 + 5;
struct tree {
  int ta, mi;
} t[N << 2];
int a[N], pre[N], L, R, cnt, ans, col, lx[N], rx[N], vis[N], nxt[N];
int read() {
  int s = 0;
  char ch = getchar();
  while (!isdigit(ch)) ch = getchar();
  while (isdigit(ch)) s = s * 10 + ch - '0', ch = getchar();
  return s;
}
void build(int ro, int l, int r) {
  if (l == r) {
    t[ro].mi = l;
    return;
  }
  build(ro << 1, l, ((l + r) >> 1)), build(ro << 1 | 1, ((l + r) >> 1) + 1, r);
  t[ro].mi = min(t[ro << 1].mi, t[ro << 1 | 1].mi);
}
inline void pushdown(int ro) {
  if (t[ro].ta) {
    t[ro << 1].mi += t[ro].ta, t[ro << 1 | 1].mi += t[ro].ta;
    t[ro << 1].ta += t[ro].ta, t[ro << 1 | 1].ta += t[ro].ta;
    t[ro].ta = 0;
  }
}
void updat(int ro, int l, int r, int L, int R) {
  if (L <= l && r <= R) {
    t[ro].mi--, t[ro].ta--;
    return;
  }
  pushdown(ro);
  if (L <= ((l + r) >> 1)) updat(ro << 1, l, ((l + r) >> 1), L, R);
  if (R > ((l + r) >> 1)) updat(ro << 1 | 1, ((l + r) >> 1) + 1, r, L, R);
  t[ro].mi = min(t[ro << 1].mi, t[ro << 1 | 1].mi);
}
void work() {
  m = read();
  while (m--) {
    n = read();
    for (int i = 1; i <= n; i++)
      a[i] = read(), pre[i] = pre[i - 1] + (a[i] == 0), vis[a[i]] = 1;
    for (int i = n; i; i--) nxt[i] = nxt[i + 1] + (a[i] == 0);
    ans = cnt = pre[n] / 2, col = L = R = 0;
    for (int i = 1; i <= n; i++)
      if (pre[i] <= cnt) L++;
    R = L + 1;
    for (int i = 1; i <= n; i++) rx[i] = n + 1;
    for (int i = 1; i <= L; i++) lx[a[i]] = i;
    for (int i = n; i >= R; i--) rx[a[i]] = i;
    for (int i = 1; i <= n; i++) col += vis[i];
    ans = min(ans, col);
    build(1, 0, nxt[R]);
    for (int i = 1; i <= n; i++)
      if (vis[i] && !lx[i])
        updat(1, 0, nxt[R], nxt[rx[i]], nxt[R]), ans = min(ans, col + t[1].mi);
    for (int i = 1; i <= L; i++)
      if (lx[a[i]] == i)
        updat(1, 0, nxt[R], nxt[rx[a[i]]], nxt[R]),
            ans = min(col + pre[i] + t[1].mi, ans);
    printf("%d\n", ans);
    memset(vis, 0, sizeof(int[n + 1])), memset(pre, 0, sizeof(int[n + 1]));
    memset(nxt, 0, sizeof(int[n + 1])), memset(lx, 0, sizeof(int[n + 1]));
    memset(t, 0, sizeof(int[4 * n + 1])), memset(rx, 0, sizeof(int[n + 1]));
  }
}
}  // namespace FGF
int main() {
  FGF::work();
  return 0;
}
