#include <bits/stdc++.h>
#pragma GCC optimize("Ofast", "unroll-loops", "omit-frame-pointer", "inline")
#pragma GCC option("arch=native", "tune=native", "no-zero-upper")
#pragma GCC target("avx2")
using namespace std;
template <typename T>
void maxtt(T& t1, T t2) {
  t1 = max(t1, t2);
}
template <typename T>
void mintt(T& t1, T t2) {
  t1 = min(t1, t2);
}
bool debug = 0;
int n, m, k;
int dx[4] = {0, 1, 0, -1}, dy[4] = {1, 0, -1, 0};
string direc = "RDLU";
const long long MOD2 = (long long)998244353 * (long long)998244353;
long long ln, lk, lm;
void etp(bool f = 0) {
  puts(f ? "YES" : "NO");
  exit(0);
}
void addmod(int& x, int y, int mod = 998244353) {
  x += y;
  if (x >= mod) x -= mod;
  if (x < 0) x += mod;
  assert(x >= 0 && x < mod);
}
void et(int x = -1) {
  printf("%d\n", x);
  exit(0);
}
long long fastPow(long long x, long long y, int mod = 998244353) {
  long long ans = 1;
  while (y > 0) {
    if (y & 1) ans = (x * ans) % mod;
    x = x * x % mod;
    y >>= 1;
  }
  return ans;
}
long long gcd1(long long x, long long y) { return y ? gcd1(y, x % y) : x; }
void fmain(int tid) {
  scanf("%d", &n);
  vector<int> a(n + 1), zs, pos(n + 1, 0), vis(n + 1, 0);
  for (int(i) = 1; (i) <= (int)(n); (i)++) {
    scanf("%d", &a[i]);
    if (a[i] == 0) zs.push_back(i);
  }
  if (zs.size() < 2) {
    puts("0");
    return;
  }
  int z = zs[(int)zs.size() / 2];
  for (int(i) = 1; (i) <= (int)(z - 1); (i)++)
    if (a[i]) pos[a[i]] = i;
  priority_queue<int> q;
  int ans = 0;
  int cc = 0;
  for (int i = z + 1; i <= n; i++) {
    if (a[i]) {
      if (!vis[a[i]]) {
        if (pos[a[i]])
          q.push(-pos[a[i]]);
        else
          cc++;
        vis[a[i]] = 1;
      }
    } else {
      if (cc) {
        ans++;
        cc--;
        continue;
      }
      if (!q.empty()) {
        int w = -q.top();
        q.pop();
        ans++;
        assert(pos[a[w]] == w);
        pos[a[w]] = 0;
      }
    }
  }
  cc = 0;
  for (int i = z - 1; i; i--) {
    if (a[i]) {
      if (pos[a[i]] == i) cc++;
    } else {
      if (cc) {
        cc--;
        ans++;
      }
    }
  }
  mintt(ans, (int)zs.size() / 2);
  printf("%d\n", ans);
}
int main() {
  int t = 1;
  scanf("%d", &t);
  for (int(i) = 1; (i) <= (int)(t); (i)++) {
    fmain(i);
  }
  return 0;
}
