#include <bits/stdc++.h>
using namespace std;
int q, n;
struct node {
  int a;
  int f;
} c[200005];
bool cmp(node n1, node n2) {
  if (n1.a == n2.a)
    return n1.f > n2.f;
  else
    return n1.a > n2.a;
}
int num;
priority_queue<int> qq;
int main() {
  cin >> q;
  while (q--) {
    cin >> n;
    for (int i = 1; i <= n; i++) {
      c[i].a = 0;
      c[i].f = 0;
    }
    int type_id, f;
    for (int i = 0; i < n; i++) {
      cin >> type_id;
      c[type_id].a++;
      cin >> f;
      if (f) c[type_id].f++;
    }
    sort(c + 1, c + 1 + n, cmp);
    int i = 1, cnt = 0, f_sum = 0;
    num = c[1].a;
    while (!qq.empty()) qq.pop();
    while ((!qq.empty() || c[i].a > 0) && num > 0) {
      if (num <= c[i].a) {
        for (; i <= n; i++) {
          if (c[i].a >= num)
            qq.push(c[i].f);
          else
            break;
        }
        cnt += num;
        f_sum += min(num, qq.top());
        qq.pop();
        num--;
      } else {
        if (!qq.empty()) {
          cnt += num;
          f_sum += min(num, qq.top());
          qq.pop();
          num--;
        } else {
          num = c[i].a;
          cnt += num;
          f_sum += min(num, c[i].f);
          num--;
          i++;
        }
      }
    }
    cout << cnt << " " << f_sum;
    cout << endl;
  }
  return 0;
}
