#include <bits/stdc++.h>
using namespace std;
using ll = long long;
using ld = long double;
int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(nullptr);
  ll q;
  cin >> q;
  for (ll i = 0; i < q; i++) {
    ll n;
    cin >> n;
    map<ll, pair<ll, ll>> m;
    for (ll j = 0; j < n; j++) {
      ll a, b;
      cin >> a >> b;
      m[a].first++;
      m[a].second += b;
    }
    priority_queue<pair<ll, ll>> todo;
    for (auto e : m) todo.push(e.second);
    ll next = 0x3FFFFFFFFFFF;
    priority_queue<ll> active;
    ll a = 0;
    ll b = 0;
    while ((!todo.empty() || !active.empty()) && next > 0) {
      if (active.empty()) next = min(next, todo.top().first);
      if (next == 0) break;
      while (!todo.empty() && todo.top().first == next) {
        active.push(todo.top().second);
        todo.pop();
      }
      a += next;
      b += min(next, active.top());
      active.pop();
      next--;
    }
    cout << a << " " << b << endl;
  }
}
