#include <bits/stdc++.h>
using namespace std;
bool sortsecond(const pair<int, int> &a, const pair<int, int> &b) {
  if (a.first == b.first) {
    return a.second < b.second;
  } else {
    return a.first < b.first;
  }
}
int main() {
  long long int t;
  cin >> t;
  while (t--) {
    long long int n;
    cin >> n;
    pair<long long int, long long int> a[n + 1];
    fill(a, a + n + 1, make_pair(0, 0));
    long long int k;
    std::vector<long long int> v;
    long long int l;
    for (long long int i = 0; i < n; i++) {
      cin >> k >> l;
      a[k].first++;
      a[k].second += l;
      if (a[k].first == 1) {
        v.push_back(k);
      }
    }
    priority_queue<pair<long long int, long long int> > a2;
    long long int g = 0;
    for (long long int i = 0; i < v.size(); i++) {
      if (a[v[i]].first != 0) {
        a2.push(a[v[i]]);
      }
    }
    long long int sum = a2.top().first;
    long long int j;
    long long int c = a2.top().first;
    long long int ans = min(a2.top().second, c);
    a2.pop();
    pair<long long int, long long int> gf;
    if (a2.empty()) {
      cout << sum << " " << ans << "\n";
      continue;
    }
    while (a2.top().first > (c - 1)) {
      gf = a2.top();
      a2.pop();
      a2.push(make_pair(c - 1, gf.second));
    }
    while (a2.empty() != 1) {
      long long int sum2 = sum;
      sum += min(c - 1, a2.top().first);
      c = min(c - 1, a2.top().first);
      ans += min(a2.top().second, c);
      a2.pop();
      if (a2.empty()) {
        break;
      }
      while (a2.top().first > (c - 1)) {
        gf = a2.top();
        a2.pop();
        a2.push(make_pair(c - 1, gf.second));
      }
      if (sum == sum2) {
        break;
      }
    }
    cout << sum << " " << ans << "\n";
  }
}
