#include <bits/stdc++.h>
using namespace std;
long long mx;
bool cmp(pair<long long, long long> &a, pair<long long, long long> &b) {
  if (min(a.first, mx - 1) != min(b.first, mx - 1))
    return min(a.first, mx - 1) < min(b.first, mx - 1);
  else
    return a.second < b.second;
}
int main() {
  ios_base::sync_with_stdio(0);
  cin.tie(0);
  long long t, n, m, ans, f, candy, b;
  cin >> t;
  while (t--) {
    cin >> n;
    mx = 0;
    vector<pair<long long, long long>> data(n + 1);
    for (int i = 0; i < n; ++i) {
      cin >> candy >> b;
      ++data[candy].first;
      data[candy].second += b;
      mx = max(mx, data[candy].first);
    }
    sort(data.begin(), data.end());
    priority_queue<pair<long long, long long>> q;
    ans = f = 0;
    ++mx;
    while (!data.empty() && mx > 0) {
      while (!q.empty() && (data.back().first != mx - 1)) {
        b = min(q.top().second, mx - 1);
        ans += b;
        f += min(b, q.top().first);
        mx = b;
        q.pop();
      }
      mx = data.back().first + 1;
      while (!data.empty() && (data.back().first == (mx - 1))) {
        q.push({data.back().second, data.back().first});
        data.pop_back();
      }
    }
    cout << ans << " " << f << "\n";
  }
  return 0;
}
