#include <bits/stdc++.h>
using namespace std;
namespace zzc {
inline int read() {
  int x = 0, f = 1;
  char ch = getchar();
  while (!isdigit(ch)) {
    if (ch == '-') f = -1;
    ch = getchar();
  }
  while (isdigit(ch)) {
    x = x * 10 + ch - 48;
    ch = getchar();
  }
  return x * f;
}
const int maxn = 5e5 + 5;
int t, n, len, ans;
int a[maxn], lef[maxn], rig[maxn], b[maxn];
vector<int> pos[maxn];
priority_queue<int> q;
void work() {
  t = read();
  while (t--) {
    n = read();
    ans = 0;
    len = 0;
    for (register int i = 1; i <= n; i++) {
      a[i] = read();
      if (a[i] == 0) b[++len] = i;
    }
    if (len <= 1) {
      puts("0");
      continue;
    }
    int mid = b[len / 2];
    for (register int i = 1; i <= n; i++)
      rig[i] = 0, lef[i] = n + 1, pos[i].clear();
    for (register int i = 1; i <= mid; i++)
      if (a[i]) rig[a[i]] = i;
    for (register int i = n; i > mid; i--)
      if (a[i]) lef[a[i]] = i;
    for (register int i = 1; i <= n; i++) {
      rig[i] += n - mid;
      lef[i] -= mid;
      pos[lef[i]].push_back(rig[i]);
    }
    for (register int i = 1; i <= n; i++) {
      for (auto j : pos[i]) q.push(-j);
      while (!q.empty() && (-q.top()) < i) q.pop();
      if (!a[(i + mid - 1) % n + 1] && !q.empty()) ans++, q.pop();
    }
    printf("%d\n", min(ans, len / 2));
  }
}
}  // namespace zzc
int main() {
  zzc::work();
  return 0;
}
