#include <bits/stdc++.h>
using namespace std;
using namespace std;
struct Node {
  int t;
  int f;
};
bool operator<(const Node& a, const Node& b) {
  if (a.t == b.t) return a.f > b.f;
  return a.t > b.t;
}
int main() {
  std::ios::sync_with_stdio(false);
  cin.tie(nullptr);
  cout.tie(nullptr);
  int q;
  cin >> q;
  while (q--) {
    int n;
    cin >> n;
    map<int, Node> s;
    int i;
    for (i = 0; i < n; i++) {
      int ta, tb;
      cin >> ta >> tb;
      auto it = s.find(ta);
      if (it == s.end()) {
        Node tmp;
        tmp.t = 1;
        tmp.f = tb;
        s[ta] = tmp;
      } else {
        (it->second).t = (it->second).t + 1;
        (it->second).f = (it->second).f + tb;
      }
    }
    map<int, vector<Node>, greater<int>> c;
    int ans1 = 0;
    int ans2 = 0;
    for (auto& it : s) {
      auto it2 = c.find((it.second).t);
      if (it2 == c.end()) {
        vector<Node> tmp;
        tmp.push_back(it.second);
        c[(it.second).t] = tmp;
      } else {
        (it2->second).push_back(it.second);
      }
    }
    priority_queue<int> pq;
    auto it = c.begin();
    int cur = it->first;
    while (cur > 0) {
      if (it != c.end() && cur == it->first) {
        for (i = 0; i < (it->second).size(); i++) {
          pq.push((it->second)[i].f);
        }
        it++;
      }
      ans1 += cur;
      ans2 += min(pq.top(), cur);
      pq.pop();
      if (!pq.empty()) {
        cur--;
      } else if (it != c.end()) {
        cur = it->first;
      } else {
        break;
      }
    }
    cout << ans1 << " " << ans2 << endl;
  }
  return 0;
}
