#include <bits/stdc++.h>
using namespace std;
int main() {
  int n, q;
  scanf("%d", &q);
  while (q--) {
    scanf("%d", &n);
    vector<pair<int, int> > freq(n + 1, pair<int, int>(0, 0));
    for (int i = 0; i < n; i++) {
      int x, f;
      scanf("%d%d", &x, &f);
      freq[x].first++;
      freq[x].second += f;
    }
    sort(freq.begin(), freq.end(), greater<pair<int, int> >());
    int ans = 0, ans2 = 0;
    multiset<int> ms;
    int cur = freq[0].first, i = 0;
    while (cur) {
      while (freq[i].first >= cur) {
        ms.insert(freq[i].second);
        i++;
      }
      if (!ms.empty()) {
        auto it = ms.rbegin();
        int x = *it;
        ms.erase(ms.find(x));
        ans += cur;
        ans2 += min(x, cur);
        cur--;
      } else
        cur--;
    }
    while (cur && !ms.empty()) {
      auto it = ms.rbegin();
      int x = *it;
      ms.erase(ms.find(x));
      ans += cur;
      ans2 += x;
      cur--;
    }
    printf("%d %d\n", ans, ans2);
  }
  return 0;
}
