#include <bits/stdc++.h>
using namespace std;
int maxMatching(const vector<pair<int, int>>& vec, int bn) {
  int an = vec.size();
  priority_queue<pair<int, int>, vector<pair<int, int>>,
                 greater<pair<int, int>>>
      pq;
  vector<int> match(an, -1), as(an);
  iota(as.begin(), as.end(), 0);
  sort(as.begin(), as.end(),
       [&](int i, int j) { return vec[i].first > vec[j].first; });
  int matches = 0;
  for (int b = bn - 1, a = 0; b >= 0; --b) {
    while (a < an && vec[as[a]].first > b) {
      pq.push({vec[as[a]].second, as[a]});
      ++a;
    }
    if (!pq.empty()) {
      match[pq.top().second] = b;
      ++matches;
      pq.pop();
    }
  }
  sort(as.begin(), as.end(),
       [&](int i, int j) { return vec[i].second > vec[j].second; });
  int lastMatch = *max_element(match.begin(), match.end());
  for (int i : as) {
    if (match[i] != -1 || lastMatch == bn - 1 || !vec[i].second) continue;
    lastMatch = max(lastMatch + 1, bn - vec[i].second);
    match[i] = lastMatch;
    ++matches;
  }
  return matches;
}
int main() {
  int T;
  scanf("%d", &T);
  while (T--) {
    int n;
    scanf("%d", &n);
    vector<int> A(n);
    vector<pair<int, int>> vec(n + 1);
    int z = 0;
    for (int i = 0; i < n; ++i) {
      scanf("%d", &A[i]);
      z += A[i] == 0;
    }
    int maxAns = z / 2;
    int curz = 0;
    for (int i = 0; i < n; ++i) {
      curz += A[i] == 0;
      if (A[i]) {
        int remz = z - curz;
        if (curz >= maxAns)
          vec[A[i]].second = max(vec[A[i]].second, min(maxAns, remz));
        if (remz >= maxAns)
          vec[A[i]].first = max(vec[A[i]].first, min(maxAns, curz));
      }
    }
    int ans = maxMatching(vec, z);
    ans = min(ans, maxAns);
    printf("%d\n", ans);
  }
}
