#include <bits/stdc++.h>
using namespace std;
void solve() {
  map<int, pair<int, int> > mapa;
  int n;
  cin >> n;
  for (int i = 0; i < n; i++) {
    int a, f;
    cin >> a >> f;
    if (not mapa.count(a)) mapa[a] = pair<int, int>(0, 0);
    mapa[a].first++;
    mapa[a].second += f;
  }
  vector<pair<int, int> > v;
  for (map<int, pair<int, int> >::iterator it = mapa.begin(); it != mapa.end();
       it++)
    v.push_back(it->second);
  sort(v.begin(), v.end());
  int s = 0;
  int s1 = 0;
  priority_queue<int> q;
  int cur = 1000000000;
  for (int i = int(v.size()) - 1; i >= 0 and cur > 0;) {
    if (not(v[i].first < cur and int(q.size()))) {
      cur = v[i].first;
      for (; i >= 0 and v[i].first == cur; i--) q.push(v[i].second);
    }
    s += cur;
    s1 += min(cur, q.top());
    q.pop();
    cur--;
  }
  while (cur > 0 and int(q.size())) {
    s += cur;
    s1 += min(cur, q.top());
    q.pop();
    cur--;
  }
  cout << s << " " << s1 << endl;
}
int cases;
int main() {
  ios::sync_with_stdio(false);
  cin >> cases;
  for (int cas = 0; cas < cases; cas++) solve();
}
