#include <bits/stdc++.h>
using namespace std;
pair<int, int> v[200005];
int main() {
  cin.tie(0)->sync_with_stdio(0);
  cin.tie(NULL);
  cout.tie(NULL);
  int T;
  cin >> T;
  while (T--) {
    int n;
    cin >> n;
    for (int i = 1; i <= n; i++) {
      int x, type;
      cin >> x >> type;
      v[x].first++;
      v[x].second += type;
    }
    sort(v + 1, v + n + 1);
    priority_queue<int> q;
    int crt = n;
    int l = n;
    int ans1 = 0, ans2 = 0;
    while (l > 0 && crt > 0) {
      while (v[l].first >= crt) {
        q.push(v[l].second);
        l--;
      }
      if (!q.empty()) {
        ans1 += crt;
        ans2 += min(crt, q.top());
        q.pop();
      }
      crt--;
    }
    for (int i = 1; i <= n; i++) v[i].first = v[i].second = 0;
    cout << ans1 << " " << ans2 << '\n';
  }
}
