#include <bits/stdc++.h>
using namespace std;
void solve() {
  int n;
  cin >> n;
  vector<int> v(n);
  for (auto &first : v) cin >> first;
  int z = 0;
  for (auto first : v)
    if (first == 0) z++;
  if (z <= 1) {
    cout << "0\n";
    return;
  }
  vector<pair<int, int> > cnt(n, make_pair(0, 0));
  int u = 0;
  for (int i = 0; i < n; ++i) {
    if (v[i] == 0)
      u++;
    else {
      cnt[v[i] - 1].first = max(cnt[v[i] - 1].first, min(u, z / 2));
    }
    if (u > z - z / 2) {
      break;
    }
  }
  u = 0;
  for (int i = 0; i < n; ++i) {
    if (v[n - i - 1] == 0)
      u++;
    else {
      cnt[v[n - i - 1] - 1].second =
          max(cnt[v[n - i - 1] - 1].second, min(u, z / 2));
    }
    if (u > z - z / 2) {
      break;
    }
  }
  sort(cnt.begin(), cnt.end(), greater<pair<int, int> >());
  int ans = 0;
  multiset<int> rb;
  int ptr = 0;
  for (int i = z / 2; i > 0; i--) {
    while (ptr < cnt.size() && cnt[ptr].first == i) {
      rb.insert(cnt[ptr].second);
      ptr++;
    }
    if (rb.size()) {
      rb.erase(rb.begin());
      ans++;
    }
  }
  while (ptr < cnt.size()) {
    rb.insert(cnt[ptr].second);
    ptr++;
  }
  for (int i = 1; i <= z / 2; ++i) {
    while (rb.size()) {
      auto it = rb.begin();
      auto val = (*it);
      if (val < i)
        rb.erase(it);
      else
        break;
    }
    if (rb.size()) {
      rb.erase(rb.begin());
      ans++;
    }
  }
  cout << min(ans, z / 2) << '\n';
}
int main() {
  ios_base::sync_with_stdio(0);
  cin.tie(0);
  int t;
  cin >> t;
  for (int i = 0; i < t; ++i) solve();
}
