#include <bits/stdc++.h>
using namespace std;
struct Node {
  int x, y;
};
const int N = 2e5 + 10;
Node a[N];
priority_queue<int> pq;
int main() {
  int T;
  scanf("%d", &T);
  int n, x, y, p, ans1, ans2;
  while (T--) {
    scanf("%d", &n);
    for (int i = 1; i <= n; i++) a[i].x = a[i].y = 0;
    for (int i = 1; i <= n; i++) {
      scanf("%d %d", &x, &y);
      a[x].x++;
      if (y) a[x].y++;
    }
    sort(a + 1, a + n + 1, [&](Node x, Node y) {
      if (x.x == y.x) return x.y < y.y;
      return x.x < y.x;
    });
    while (!pq.empty()) pq.pop();
    p = n;
    ans1 = ans2 = 0;
    for (int i = n; i >= 1; i--) {
      while (p && a[p].x >= i) pq.push(a[p--].y);
      if (!pq.empty()) {
        ans1 += i;
        ans2 += min(pq.top(), i);
        pq.pop();
      }
    }
    printf("%d %d\n", ans1, ans2);
  }
  return 0;
}
