Problem: You are given a sequence a_1, a_2, …, a_n of non-negative integers.

You need to find the largest number m of triples (i_1, j_1, k_1), (i_2, j_2, k_2), ..., (i_m, j_m, k_m) such that:

  * 1 ≤ i_p < j_p < k_p ≤ n for each p in 1, 2, …, m;
  * a_{i_p} = a_{k_p} = 0, a_{j_p} ≠ 0;
  * all a_{j_1}, a_{j_2}, …, a_{j_m} are different;
  * all i_1, j_1, k_1, i_2, j_2, k_2, …, i_m, j_m, k_m are different.

Input

The first line of input contains one integer t (1 ≤ t ≤ 500 000): the number of test cases.

The first line of each test case contains one integer n (1 ≤ n ≤ 500 000).

The second line contains n integers a_1, a_2, …, a_n (0 ≤ a_i ≤ n).

The total sum of n is at most 500 000.

Output

For each test case, print one integer m: the largest number of proper triples that you can find.

Example

Input


8
1
1
2
0 0
3
0 1 0
6
0 0 1 2 0 0
6
0 1 0 0 1 0
6
0 1 3 2 0 0
6
0 0 0 0 5 0
12
0 1 0 2 2 2 0 0 3 3 4 0


Output


0
0
1
2
1
1
1
2

Note

In the first two test cases, there are not enough elements even for a single triple, so the answer is 0.

In the third test case we can select one triple (1, 2, 3).

In the fourth test case we can select two triples (1, 3, 5) and (2, 4, 6).

In the fifth test case we can select one triple (1, 2, 3). We can't select two triples (1, 2, 3) and (4, 5, 6), because a_2 = a_5.

Solution (in CPP):

Solution:
#include <bits/stdc++.h>
using namespace std;
template <typename T>
void debug_out(T t) {
  cerr << t;
}
template <typename A, typename B>
void debug_out(pair<A, B> u) {
  cerr << "(" << u.first << " " << u.second << ")";
}
template <typename T>
void debug_out(vector<T> t) {
  int sz = t.size();
  for (int i = 0; i < sz; i++) {
    debug_out(t[i]);
    if (i != sz - 1) cerr << ", ";
  }
}
template <typename T>
void debug_out(vector<vector<T>> t) {
  int sz = t.size();
  for (int i = 0; i < sz; i++) {
    debug_out(t[i]);
    if (i != sz - 1) cerr << endl;
  }
}
template <typename Head, typename... Tail>
void debug_out(Head H, Tail... T) {
  debug_out(H);
  cerr << " ";
  debug_out(T...);
}
template <typename T>
void debug_out(set<T> a) {
  vector<T> _a;
  for (T t : a) _a.push_back(t);
  debug_out(_a);
}
class segtree {
 public:
  struct node {
    int val = 0;
    int ad = 0;
    int sz = 1;
    void apply(int l, int r, int v) {
      val += v;
      ad += v;
    }
  };
  node unite(const node &a, const node &b) const {
    node res;
    res.val = min(a.val, b.val);
    res.sz = a.sz + b.sz;
    return res;
  }
  inline void push(int x, int l, int r) {
    int y = (l + r) >> 1;
    int z = x + ((y - l + 1) << 1);
    if (tree[x].ad != 0) {
      tree[x + 1].apply(l, y, tree[x].ad);
      tree[z].apply(y + 1, r, tree[x].ad);
      tree[x].ad = 0;
    }
  }
  inline void pull(int x, int z) { tree[x] = unite(tree[x + 1], tree[z]); }
  int n;
  vector<node> tree;
  void build(int x, int l, int r) {
    if (l == r) {
      return;
    }
    int y = (l + r) >> 1;
    int z = x + ((y - l + 1) << 1);
    build(x + 1, l, y);
    build(z, y + 1, r);
    pull(x, z);
  }
  template <typename M, typename... T>
  void build(int x, int l, int r, const vector<M> &v, const T &...t) {
    if (l == r) {
      tree[x].apply(l, r, v[l], t...);
      return;
    }
    int y = (l + r) >> 1;
    int z = x + ((y - l + 1) << 1);
    build(x + 1, l, y, v, t...);
    build(z, y + 1, r, v, t...);
    pull(x, z);
  }
  template <typename M, typename... T>
  segtree(const vector<M> &v, const T &...t) {
    n = v.size();
    assert(n > 0);
    tree.resize(2 * n - 1);
    build(0, 0, n - 1, v, t...);
  }
  segtree(int _n) : n(_n) {
    assert(n > 0);
    tree.resize(2 * n - 1);
    build(0, 0, n - 1);
  }
  segtree(){};
  node get(int x, int l, int r, int ll, int rr) {
    if (ll <= l && r <= rr) {
      return tree[x];
    }
    int y = (l + r) >> 1;
    int z = x + ((y - l + 1) << 1);
    push(x, l, r);
    node res{};
    if (rr <= y) {
      res = get(x + 1, l, y, ll, rr);
    } else {
      if (ll > y) {
        res = get(z, y + 1, r, ll, rr);
      } else {
        res = unite(get(x + 1, l, y, ll, rr), get(z, y + 1, r, ll, rr));
      }
    }
    pull(x, z);
    return res;
  }
  node get(int ll, int rr) {
    assert(0 <= ll && ll <= rr && rr <= n - 1);
    return get(0, 0, n - 1, ll, rr);
  }
  node get(int p) {
    assert(0 <= p && p <= n - 1);
    return get(0, 0, n - 1, p, p);
  }
  template <typename... M>
  void modify(int x, int l, int r, int ll, int rr, const M &...v) {
    if (ll <= l && r <= rr) {
      tree[x].apply(l, r, v...);
      return;
    }
    int y = (l + r) >> 1;
    int z = x + ((y - l + 1) << 1);
    push(x, l, r);
    if (ll <= y) {
      modify(x + 1, l, y, ll, rr, v...);
    }
    if (rr > y) {
      modify(z, y + 1, r, ll, rr, v...);
    }
    pull(x, z);
  }
  template <typename... M>
  void modify(int ll, int rr, const M &...v) {
    assert(0 <= ll && ll <= rr && rr <= n - 1);
    modify(0, 0, n - 1, ll, rr, v...);
  }
  int find_first_knowingly(int x, int l, int r,
                           const function<bool(const node &)> &f) {
    if (l == r) {
      return l;
    }
    push(x, l, r);
    int y = (l + r) >> 1;
    int z = x + ((y - l + 1) << 1);
    int res;
    if (f(tree[x + 1])) {
      res = find_first_knowingly(x + 1, l, y, f);
    } else {
      res = find_first_knowingly(z, y + 1, r, f);
    }
    pull(x, z);
    return res;
  }
  int find_first(int x, int l, int r, int ll, int rr,
                 const function<bool(const node &)> &f) {
    if (ll <= l && r <= rr) {
      if (!f(tree[x])) {
        return -1;
      }
      return find_first_knowingly(x, l, r, f);
    }
    push(x, l, r);
    int y = (l + r) >> 1;
    int z = x + ((y - l + 1) << 1);
    int res = -1;
    if (ll <= y) {
      res = find_first(x + 1, l, y, ll, rr, f);
    }
    if (rr > y && res == -1) {
      res = find_first(z, y + 1, r, ll, rr, f);
    }
    pull(x, z);
    return res;
  }
  int find_first(int ll, int rr, const function<bool(const node &)> &f) {
    assert(0 <= ll && ll <= rr && rr <= n - 1);
    return find_first(0, 0, n - 1, ll, rr, f);
  }
  int find_last_knowingly(int x, int l, int r,
                          const function<bool(const node &)> &f) {
    if (l == r) {
      return l;
    }
    push(x, l, r);
    int y = (l + r) >> 1;
    int z = x + ((y - l + 1) << 1);
    int res;
    if (f(tree[z])) {
      res = find_last_knowingly(z, y + 1, r, f);
    } else {
      res = find_last_knowingly(x + 1, l, y, f);
    }
    pull(x, z);
    return res;
  }
  int find_last(int x, int l, int r, int ll, int rr,
                const function<bool(const node &)> &f) {
    if (ll <= l && r <= rr) {
      if (!f(tree[x])) {
        return -1;
      }
      return find_last_knowingly(x, l, r, f);
    }
    push(x, l, r);
    int y = (l + r) >> 1;
    int z = x + ((y - l + 1) << 1);
    int res = -1;
    if (rr > y) {
      res = find_last(z, y + 1, r, ll, rr, f);
    }
    if (ll <= y && res == -1) {
      res = find_last(x + 1, l, y, ll, rr, f);
    }
    pull(x, z);
    return res;
  }
  int find_last(int ll, int rr, const function<bool(const node &)> &f) {
    assert(0 <= ll && ll <= rr && rr <= n - 1);
    return find_last(0, 0, n - 1, ll, rr, f);
  }
};
int tt;
int main() {
  ios::sync_with_stdio(false);
  cin.tie(0);
  cout.tie(0);
  cin >> tt;
  while (tt--) {
    int n;
    cin >> n;
    vector<int> a(n);
    for (int i = 0; i < n; i++) {
      cin >> a[i];
    }
    int zeros = count(a.begin(), a.end(), 0);
    int p = zeros / 2;
    int lf = 0, rt = zeros;
    vector<vector<pair<int, int>>> segs(n);
    for (int i = 0; i < n; i++) {
      if (a[i] == 0) {
        lf++;
        rt--;
        continue;
      }
      int rr = min(p, lf);
      int ll = p + 1 - min(p, rt);
      segs[a[i] - 1].push_back({ll - 1, rr - 1});
    }
    vector<pair<int, int>> g;
    for (int i = 0; i < n; i++) {
      int l = -1, r = p;
      for (auto j : segs[i]) {
        assert(j.first == 0 || j.second == p - 1);
        if (j.first == 0) l = max(l, j.second);
        if (j.second == p - 1) r = min(r, j.first);
      }
      if (l >= r - 1) {
        g.push_back({p, p});
      } else {
        g.push_back({l + 1, p - r});
      }
    }
    sort(g.begin(), g.end());
    int bound = 0;
    int cur = 0;
    while (true) {
      while (cur < (int)g.size() && g[cur].first <= bound) cur++;
      if (cur == (int)g.size()) break;
      cur++;
      bound++;
    }
    if (bound == p) {
      cout << p << "\n";
      continue;
    }
    multiset<int> ret;
    vector<vector<int>> add(bound);
    for (auto i : g)
      if (min(i.first, bound) > 0)
        add[min(i.first - 1, bound - 1)].push_back(i.second);
    vector<int> rev;
    for (int i = bound - 1; i >= 0; i--) {
      for (int j : add[i]) ret.insert(j);
      assert(ret.size());
      rev.push_back(*ret.begin());
      ret.erase(ret.begin());
    }
    sort(rev.begin(), rev.end());
    vector<int> foo(p);
    iota(foo.begin(), foo.end(), 0);
    segtree st(foo);
    for (auto i : g) {
      if (i.second > 0) st.modify(0, i.second - 1, 1);
    }
    for (int i : rev) {
      if (i > 0) st.modify(0, i - 1, -1);
    }
    int res = bound;
    for (int sz = bound; sz >= 0; sz--) {
      int cnt = 0;
      int pos = INT_MAX;
      int bb = st.find_first(0, p - sz - 1, [&](const segtree::node &a) {
        int v = min(pos - a.sz, a.val - (cnt + a.sz));
        if (v >= 0) {
          cnt += a.sz;
          pos = v;
          return false;
        }
        return true;
      });
      res = max(res, sz + cnt);
      if (sz == 0) break;
      if (rev[sz - 1] > 0) st.modify(0, rev[sz - 1] - 1, 1);
    }
    cout << res << "\n";
  }
  return 0;
}