#include <bits/stdc++.h>
using namespace std;
const int N = 200004;
int a[N], b[N], n, p = 0;
map<int, pair<int, int> > mp;
vector<int> g[N];
pair<int, int> pa[N];
pair<int, int> f() {
  int s = 0, t = 0, mx, m;
  p = 0;
  for (auto it = mp.begin(); it != mp.end(); it++) pa[p++] = it->second;
  sort(pa, pa + p, greater<pair<int, int> >());
  mx = m = pa[0].first;
  priority_queue<int> q;
  for (int i = 0; i < p; i++) {
    g[pa[i].first].push_back(pa[i].second);
  }
  for (int i = mx; i >= 1; i--) {
    for (int x : g[i]) q.push(x);
    if (!q.empty()) {
      s += i;
      t += min(q.top(), i);
      q.pop();
    }
  }
  for (int i = 1; i <= m; i++) g[i].clear();
  return pair<int, int>(s, t);
}
int main() {
  int T;
  vector<int> u, v;
  scanf("%d", &T);
  while (T--) {
    scanf("%d", &n);
    for (int i = 1; i <= n; i++) scanf("%d %d", &a[i], &b[i]);
    mp.clear();
    for (int i = 1; i <= n; i++) {
      ++mp[a[i]].first;
      if (b[i]) ++mp[a[i]].second;
    }
    pair<int, int> pa = f();
    printf("%d %d\n", pa.first, pa.second);
  }
}
