#include <bits/stdc++.h>
using namespace std;
long long q;
long long n, a[202020], cnt[202020], mx, f[202020];
signed main() {
  ios::sync_with_stdio(false);
  cin.tie(0);
  cout.tie();
  cin >> q;
  while (q--) {
    cin >> n;
    set<long long> s;
    multiset<pair<long long, long long>> t;
    long long ans = 0, fans = 0;
    mx = 0;
    for (long long i = 1; i <= n; i++) {
      long long x;
      cin >> a[i] >> x;
      if (x == 0) {
        f[a[i]]++;
      }
      s.insert(a[i]);
      cnt[a[i]]++;
      mx = max(mx, cnt[a[i]]);
    }
    for (auto u : s) {
      t.insert({cnt[u], f[u]});
    }
    for (long long i = mx; i >= 1; i--) {
      pair<long long, long long> p = {0, 0};
      long long cur = 1e18;
      for (auto u : t) {
        if ((u).first >= i) {
          long long dif = max(1LL * 0, i - ((u.first) - (u.second)));
          if (dif <= cur) {
            p = u;
            cur = dif;
          }
        }
      }
      if (p.first != 0) {
        ans += i;
        t.erase(t.find(p));
        fans += cur;
      }
    }
    cout << ans << " " << ans - fans << endl;
    for (auto u : s) {
      cnt[u] = 0;
      f[u] = 0;
    }
    t.clear();
    s.clear();
  }
  return 0;
}
