#include <bits/stdc++.h>
using namespace std;
int n;
pair<int, int> f[200005];
int main() {
  int q;
  scanf("%d", &q);
  while (q--) {
    scanf("%d", &n);
    for (int i = 1; i <= n; ++i) f[i] = make_pair(0, 0);
    for (int i = 1; i <= n; ++i) {
      int x, t;
      scanf("%d%d", &x, &t);
      ++f[x].first;
      if (t) ++f[x].second;
    }
    priority_queue<pair<int, int> > q;
    for (int i = 1; i <= n; ++i) q.push(f[i]);
    int pre = 1000000007;
    int m = 0;
    int ans = 0;
    while (!q.empty()) {
      if (pre < 1) break;
      pair<int, int> tmp = q.top();
      q.pop();
      if (tmp.first >= pre) {
        q.push(make_pair(pre - 1, min(tmp.second, pre - 1)));
        continue;
      } else {
        ans += tmp.first;
        m += tmp.second;
        pre = tmp.first;
      }
    }
    printf("%d %d\n", ans, m);
  }
  return 0;
}
