#include <bits/stdc++.h>
#pragma GCC target("avx2")
#pragma GCC optimization("O3")
#pragma GCC optimization("unroll-loops")
using namespace std;
void solve();
signed main() {
  ios_base::sync_with_stdio(0);
  cin.tie(0);
  cout << fixed << setprecision(10);
  int t = 1;
  cin >> t;
  while (t--) solve();
  return 0;
}
void solve() {
  int n;
  cin >> n;
  vector<int> a(n);
  vector<int> b(n);
  for (int i = 0; i < n; i++) cin >> a[i] >> b[i];
  vector<int> freqa(n + 5, 0);
  vector<int> freqb(n + 5, 0);
  for (int i = 0; i < n; i++) {
    freqa[a[i]]++;
    if (b[i]) freqb[a[i]]++;
  }
  vector<pair<int, int> > full;
  for (int i = 0; i < n + 5; i++) {
    if (freqa[i] != 0) {
      full.push_back({freqa[i], freqb[i]});
    }
  }
  sort((full).begin(), (full).end(), greater<pair<int, int> >());
  multiset<int> vals;
  int cur = 0;
  int a1 = 0, a2 = 0;
  for (int i = n + 1 - 1; i >= 1; i--) {
    if (cur < full.size() && full[cur].first >= i) {
      while (cur < full.size() && full[cur].first >= i) {
        vals.insert(full[cur].second);
        cur++;
      }
    }
    (vals);
    if (!vals.empty()) {
      a1 += i;
      auto top = --vals.end();
      a2 += min(i, *top);
      vals.erase(top);
    }
    (vals);
    (i);
    (cur);
    (a1);
    (a2);
    ;
  }
  cout << a1 << " " << a2 << "\n";
  ;
}
