#include <bits/stdc++.h>
using namespace std;
struct sss {
  long long x, y;
  bool operator<(const struct sss &a) const {
    if (y == a.y) return x < a.x;
    return y < a.y;
  }
};
long long q, n;
int main() {
  cin >> q;
  while (q--) {
    map<long long, struct sss> mp;
    priority_queue<struct sss> q;
    cin >> n;
    long long x, y;
    for (int i = 0; i < n; i++) {
      cin >> x >> y;
      if (y == 1) {
        mp[x].x++;
        mp[x].y++;
      } else
        mp[x].y++;
    }
    for (map<long long, struct sss>::iterator it = mp.begin(); it != mp.end();
         it++) {
      q.push(it->second);
    }
    long long sum = q.top().y, num = q.top().x;
    long long dq = q.top().y - 1;
    q.pop();
    while (!q.empty()) {
      struct sss z = q.top();
      q.pop();
      if (dq <= 0) break;
      if (z.y > dq) {
        long long c = z.y - dq;
        z.y -= c;
        z.x = min(z.y, z.x);
        q.push(z);
      } else {
        sum += z.y;
        dq = z.y - 1;
        num += z.x;
      }
    }
    cout << sum << " " << num << endl;
  }
}
