#include <bits/stdc++.h>
using namespace std;
const int maxn = 200020;
struct Node {
  int t, f;
  Node(){};
  Node(int _t, int _f) {
    t = _t;
    f = _f;
  }
};
Node nodes[maxn];
int cnt, q, n;
map<int, int> mp, mp2;
bool cmp(Node a, Node b) { return a.t > b.t || a.t == b.t && a.f > b.f; }
void test() {
  puts("[test]");
  for (int i = 0; i < cnt; i++) {
    printf("%d %d\n", nodes[i].t, nodes[i].f);
  }
  puts("[test end]");
}
priority_queue<int> pq;
int main() {
  scanf("%d", &q);
  while (q--) {
    scanf("%d", &n);
    mp.clear();
    cnt = 0;
    while (n--) {
      int tt, ff;
      scanf("%d%d", &tt, &ff);
      if (mp.find(tt) != mp.end()) {
        int idx = mp[tt];
        nodes[idx].t++;
        nodes[idx].f += ff;
      } else {
        mp[tt] = cnt;
        nodes[cnt++] = Node(1, ff);
      }
    }
    sort(nodes, nodes + cnt, cmp);
    long long anst = 0LL, ansf = 0LL;
    int top = INT_MAX;
    while (!pq.empty()) pq.pop();
    for (int i = 0; i < cnt; i++) {
      if (nodes[i].t > top) {
        pq.push(nodes[i].f);
      } else if (nodes[i].t < top && !pq.empty()) {
        int f2 = pq.top();
        if (f2 > top) f2 = top;
        anst += top--;
        ansf += f2;
        pq.pop();
        i--;
      } else if (nodes[i].t < top) {
        top = nodes[i].t - 1;
        anst += nodes[i].t;
        ansf += nodes[i].f;
      } else {
        int f1 = nodes[i].f;
        int f2 = 0;
        if (!pq.empty()) f2 = pq.top();
        if (f1 >= f2) {
          anst += nodes[i].t;
          ansf += nodes[i].f;
          top = nodes[i].t - 1;
        } else {
          if (f2 > top) f2 = top;
          anst += top--;
          ansf += f2;
          pq.pop();
          pq.push(nodes[i].f);
        }
      }
    }
    while (top && !pq.empty()) {
      int fff = pq.top();
      if (fff > top) fff = top;
      anst += top--;
      ansf += fff;
      pq.pop();
    }
    printf("%lld %lld\n", anst, ansf);
  }
  return 0;
}
