#include <bits/stdc++.h>
using namespace std;
const int maxn = 2e5 + 10;
int n, ans, tot, Q;
struct node {
  int c, d;
} a[maxn];
inline bool operator<(node a, node b) { return a.d < b.d; }
priority_queue<node> q;
int main() {
  scanf("%d", &Q);
  while (Q--) {
    scanf("%d", &n);
    int x, f;
    ans = tot = 0;
    for (int i = (1); i <= (n); ++i) a[i] = (node){0, 0};
    for (int i = (1); i <= (n); ++i)
      scanf("%d%d", &x, &f), a[x].c++, a[x].d += f;
    sort(a + 1, a + n + 1, [](node x, node y) { return x.c > y.c; });
    int j = 1;
    while (!q.empty()) q.pop();
    for (int i = (n); i >= (1); --i) {
      while (j <= n && a[j].c >= i) q.push(a[j]), ++j;
      if (!q.empty()) {
        node x = q.top();
        q.pop();
        ans += i;
        tot += min(x.d, i);
      }
    }
    printf("%d %d\n", ans, tot);
  }
  return 0;
}
