#include <bits/stdc++.h>
using namespace std;
int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(0);
  cout.tie(0);
  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 = 1; 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> >());
    long long cur, ans, i = 1;
    cur = ans = freq[0].first;
    vector<int> cnd;
    while (i < n && freq[i].first) {
      if (cur <= freq[i].first)
        cur--;
      else
        cur = freq[i].first;
      if (cur == 0) break;
      ans += cur;
      cnd.push_back(cur);
      i++;
    }
    long long ans2 = 0;
    multiset<int> ms;
    i = 0;
    for (int x = freq[0].first; x > 0 && i < n; x--) {
      while (i < n && freq[i].first >= x) {
        ms.insert(freq[i].second);
        i++;
      }
      if (ms.empty()) continue;
      auto it = ms.rbegin();
      int y = *it;
      ans2 += min(x, y);
      ms.erase(ms.find(y));
    }
    printf("%lld %lld\n", ans, ans2);
  }
  return 0;
}
