#include <bits/stdc++.h>
using namespace std;
const int N = 200005;
int good[N];
int cnt[N];
vector<int> types[N];
void go() {
  int n;
  cin >> n;
  for (int i = (int)(1); i <= (int)(n); i++) {
    good[i] = 0;
    cnt[i] = 0;
    types[i].clear();
  }
  for (int i = 0; i < (int)(n); i++) {
    int t, f;
    cin >> t >> f;
    cnt[t]++;
    if (f == 1) good[t]++;
  }
  for (int i = (int)(1); i <= (int)(n); i++) {
    types[cnt[i]].push_back(good[i]);
  }
  multiset<int> m;
  int a1 = 0, a2 = 0;
  for (int i = n; i >= 1; i--) {
    for (auto x : types[i]) m.insert(x);
    if (!m.empty()) {
      int r = *m.rbegin();
      a1 += i;
      a2 += min(r, i);
      m.erase(m.find(r));
    }
  }
  cout << a1 << " " << a2 << "\n";
}
int main() {
  ios_base::sync_with_stdio(0);
  cin.tie(nullptr);
  int T;
  cin >> T;
  while (T--) go();
  return 0;
}
