#include <bits/stdc++.h>
using namespace std;
const int maxn = 2e5 + 50;
const int INF = 0x3f3f3f3f;
struct node {
  int num, f;
} a[maxn];
int mp[maxn], vis[maxn], v[maxn];
struct Node {
  int num, f;
  bool operator<(const Node& tmp) const {
    if (num == tmp.num) return f < tmp.f;
    return num < tmp.num;
  }
};
priority_queue<Node> q;
int main() {
  int t;
  scanf("%d", &t);
  while (t--) {
    int n;
    scanf("%d", &n);
    int m = 0;
    for (int i = 0; i <= n; i++) {
      mp[i] = 0;
      a[i].num = 0;
      a[i].f = 0;
      vis[i] = 0;
      v[i] = 0;
    }
    for (int i = 1; i <= n; i++) {
      int k, f;
      scanf("%d%d", &k, &f);
      if (++v[k] == 1) mp[k] = ++m;
      int index = mp[k];
      a[index].num++;
      if (f == 1) a[index].f++;
    }
    long long ans = 0;
    long long ansf = 0;
    while (!q.empty()) q.pop();
    for (int i = 1; i <= m; i++) q.push({a[i].num, a[i].f});
    while (!q.empty()) {
      Node k = q.top();
      q.pop();
      if (vis[k.num] == 0) {
        vis[k.num] = 1;
        ans += k.num;
        ansf += k.f;
        continue;
      }
      while (vis[k.num] == 1 && k.num > 0) {
        k.num--;
        k.f = min(k.f, k.num);
      }
      if (k.num > 0) q.push(k);
    }
    printf("%lld %lld\n", ans, ansf);
  }
}
