#include <bits/stdc++.h>
using namespace std;
inline int read() {
  int w = 0, x = 0;
  char c = getchar();
  while (!isdigit(c)) w |= c == '-', c = getchar();
  while (isdigit(c)) x = x * 10 + (c ^ 48), c = getchar();
  return w ? -x : x;
}
namespace star {
const int maxn = 5e5 + 10;
int n, ans, sum, a[maxn << 1], l[maxn], tot, r[maxn], s;
pair<int, int> q[maxn];
inline void work() {
  n = read();
  ans = tot = s = 0;
  for (int i = 1; i <= n << 1; i++) a[i] = 0;
  for (int i = 1; i <= n; i++)
    s += (a[i] = read()) == 0, l[i] = n + 1, r[i] = 0;
  s >>= 1, sum = s;
  if (!s) return (void)puts("0");
  int x;
  for (x = 1; x <= n; x++)
    if (!a[x] and (--s == 0)) break;
  for (int i = 1; i <= x; i++) r[a[i]] = i;
  for (int i = n; i > x; i--) l[a[i]] = i;
  for (int i = 1; i <= n; i++) q[++tot] = make_pair(l[i] - x, r[i] + n - x);
  sort(q + 1, q + 1 + tot);
  priority_queue<int, vector<int>, greater<int>> que;
  for (int j = 1, i = 1; i <= n; i++)
    if (!a[(i + x - 1) % n + 1]) {
      while (j <= tot and q[j].first <= i) que.push(q[j++].second);
      while (!que.empty() and que.top() < i) que.pop();
      if (!que.empty()) ++ans, que.pop();
    }
  printf("%d\n", min(ans, sum));
}
}  // namespace star
signed main() {
  int T = read();
  while (T--) star::work();
  return 0;
}
