#include <bits/stdc++.h>
using namespace std;
const int len = 2e5 + 10;
const double pi = acos(-1.0);
const long long mod = 998244353;
const int inf = 0x3f3f3f3f;
const int maxn = 1e9 + 5;
struct node {
  int x, f;
} s[len];
int res[len];
priority_queue<int> p;
bool cmp(node x, node y) { return x.x > y.x; }
int main() {
  int t;
  scanf("%d", &t);
  while (t--) {
    int n;
    scanf("%d", &n);
    for (int i = 1; i <= n; i++) s[i].x = 0, s[i].f = 0;
    for (int i = 1; i <= n; i++) {
      int x, f;
      scanf("%d%d", &x, &f);
      s[x].x++;
      if (f == 1) s[x].f++;
    }
    sort(s + 1, s + n + 1, cmp);
    while (!p.empty()) {
      p.pop();
    }
    long long ans = 0;
    res[0] = s[1].x + 1;
    for (int i = 1; i <= n; i++) {
      res[i] = min(res[i - 1] - 1, s[i].x);
      ans += res[i];
      if (res[i] == 0) break;
    }
    long long num = 0;
    int pos = 1;
    for (int i = 1; i <= n; i++) {
      if (res[i] == 0) break;
      for (; (pos <= n) && (s[pos].x >= res[i]); pos++) {
        p.push(s[pos].f);
      }
      num += min(p.top(), res[i]);
      p.pop();
    }
    printf("%lld %lld\n", ans, num);
  }
  return 0;
}
