#include <bits/stdc++.h>
using namespace std;
const int maxn = 2e5 + 5;
inline int read() {
  char c = getchar();
  int t = 0, f = 1;
  while (!isdigit(c)) {
    if (c == '-') f = -1;
    c = getchar();
  }
  while (isdigit(c)) {
    t = (t << 3) + (t << 1) + (c ^ 48);
    c = getchar();
  }
  return t * f;
}
int q, n, a[maxn], vis[maxn];
struct node {
  int val, cnt;
} t[maxn];
bool cmp(node a, node b) {
  return a.val == b.val ? a.cnt > b.cnt : a.val < b.val;
}
bool operator<(node a, node b) { return a.cnt < b.cnt; }
priority_queue<node> qu;
int main() {
  q = read();
  while (q--) {
    n = read();
    while (!qu.empty()) qu.pop();
    for (int i = 1; i <= n; i++) t[i].val = t[i].cnt = 0, vis[i] = 0;
    for (int i = 1; i <= n; i++) {
      a[i] = read();
      int x = read();
      t[a[i]].val++;
      t[a[i]].cnt += x;
    }
    sort(t + 1, t + 1 + n, cmp);
    int mx = t[n].val;
    int ans = 0, tmp = 0, pos = n;
    for (int i = mx; i >= 1; i--) {
      while (t[pos].val >= i) {
        qu.push(t[pos]);
        pos--;
      }
      if (!qu.empty()) {
        node u = qu.top();
        qu.pop();
        ans += i;
        tmp += min(i, u.cnt);
      }
    }
    printf("%d %d\n", ans, tmp);
  }
  return 0;
}
