#include <bits/stdc++.h>
using namespace std;
long long q, n, x, pri, pre, ans, ac;
int main() {
  ios::sync_with_stdio(false);
  cin.tie(0);
  cout.tie(0);
  cin >> q;
  while (q--) {
    cin >> n;
    set<long long> s, p;
    vector<long long> cnt(n + 1), cntpri(n + 1), next(n + 1);
    vector<vector<long long>> e(n + 1);
    for (int i = 0; i < n; i++) {
      cin >> x >> pri;
      s.insert(x);
      cnt[x]++;
      cntpri[x] += pri;
    }
    for (int u : s) {
      e[cnt[u]].push_back(cntpri[u]);
      p.insert(cnt[u]);
    }
    vector<long long> v(p.begin(), p.end());
    for (int i = (int)v.size() - 1; i > 0; i--) next[v[i]] = v[i - 1];
    ans = 0, ac = 0;
    for (int i = (int)v.size() - 1; i >= 0; i--) {
      if (e[v[i]].size() > v[i] - next[v[i]]) {
        sort(e[v[i]].begin(), e[v[i]].end());
        reverse(e[v[i]].begin(), e[v[i]].end());
        while (e[v[i]].size() > v[i] - next[v[i]]) {
          e[next[v[i]]].push_back(e[v[i]].back());
          e[v[i]].pop_back();
        }
      }
      sort(e[v[i]].begin(), e[v[i]].end());
      reverse(e[v[i]].begin(), e[v[i]].end());
      pre = v[i];
      for (int j = 0; j < e[v[i]].size(); j++) {
        ans += pre;
        if (e[v[i]][j] > pre)
          ac += pre;
        else
          ac += e[v[i]][j];
        pre--;
      }
    }
    cout << ans << " " << ac << "\n";
  }
  return 0;
}
