#include <bits/stdc++.h>
using namespace std;
const int N = 5e5 + 10, size = 1 << 20, mod = 998244353, inf = 2e9;
const long long INF = 1e15;
template <class o>
void qr(o& x) {
  char c = getchar();
  x = 0;
  int f = 1;
  while (!isdigit(c)) {
    if (c == '-') f = -1;
    c = getchar();
  }
  while (isdigit(c)) x = x * 10 + c - '0', c = getchar();
  x *= f;
}
template <class o>
void qw(o x) {
  if (x / 10) qw(x / 10);
  putchar(x % 10 + '0');
}
template <class o>
void pr1(o x) {
  if (x < 0) x = -x, putchar('-');
  qw(x);
  putchar(' ');
}
template <class o>
void pr2(o x) {
  if (x < 0) x = -x, putchar('-');
  qw(x);
  putchar(10);
}
template <class o>
void cmax(o& x, o y) {
  if (x < y) x = y;
}
void cmax(int& x, int y) { x = x - y >> 31 ? y : x; }
template <class o>
void cmin(o& x, o y) {
  if (x > y) x = y;
}
void cmin(int& x, int y) { x = x - y >> 31 ? x : y; }
int n, m, cnt, a[N], L[N], R[N], zero, pre[N], nxt[N], type[N], ans, vis[N];
int mn[N << 2], tag[N << 2];
void bt(int x, int l, int r) {
  tag[x] = 0;
  if (l == r) {
    mn[x] = l + cnt;
    return;
  }
  int mid = (l + r) / 2;
  bt((x << 1), l, mid);
  bt((x << 1 | 1), mid + 1, r);
  cmin(mn[x] = mn[(x << 1)], mn[(x << 1 | 1)]);
}
void down(int x, int y) {
  mn[x] += y;
  tag[x] += y;
}
void pushdown(int x) {
  if (tag[x]) down((x << 1), tag[x]), down((x << 1 | 1), tag[x]), tag[x] = 0;
}
void del(int x, int l, int r, int L, int R) {
  if (L <= l && r <= R) return down(x, -1);
  int mid = (l + r) / 2;
  pushdown(x);
  if (L <= mid) del((x << 1), l, mid, L, R);
  if (mid < R) del((x << 1 | 1), mid + 1, r, L, R);
  cmin(mn[x] = mn[(x << 1)], mn[(x << 1 | 1)]);
}
void solve() {
  qr(n);
  nxt[n + 1] = 0;
  for (auto i = 1; i <= n; i++) qr(a[i]), vis[i] = L[i] = R[i] = 0;
  for (auto i = 1; i <= n; i++) pre[i] = pre[i - 1] + !a[i];
  for (auto i = n; i >= 1; i--) nxt[i] = nxt[i + 1] + !a[i];
  zero = pre[n] / 2;
  ans = zero;
  for (auto i = 1; i <= n; i++) type[i] = pre[i] <= zero ? 1 : 2;
  for (auto i = 1; i <= n; i++) vis[a[i]] = 1;
  for (auto i = 1; i <= n; i++)
    if (type[i] == 1) L[a[i]] = i;
  for (auto i = n; i >= 1; i--)
    if (type[i] == 2) R[a[i]] = i;
  cnt = 0;
  for (auto i = 1; i <= n; i++) cnt += vis[i];
  bt(1, 0, m = pre[n] - zero);
  for (auto i = 1; i <= n; i++)
    if (!L[i] && R[i]) del(1, 0, m, nxt[R[i]], m);
  cmin(ans, mn[1]);
  for (auto i = 1; i <= n; i++) {
    if (type[i] == 2) break;
    if (a[i] && L[a[i]] == i) {
      del(1, 0, m, nxt[R[a[i]]], m);
      cmin(ans, mn[1] + pre[i]);
    }
  }
  pr2(ans);
}
int main() {
  int T = 1;
  qr(T);
  while (T--) solve();
  return 0;
}
