#include <bits/stdc++.h>
using namespace std;
long long q, n, a[200005], f[200005], val1, val2, last, ans1, ans2, cont;
vector<pair<long long, long long> > tmp;
priority_queue<long long> pq;
int main() {
  ios::sync_with_stdio(false);
  cin.tie(0);
  cin >> q;
  while (q--) {
    cin >> n;
    ans1 = ans2 = 0;
    for (int i = 0; i <= n; i++) a[i] = f[i] = 0;
    for (int i = 0; i < n; i++) {
      cin >> val1 >> val2;
      a[val1]++;
      if (val2 == 1) f[val1]++;
    }
    tmp.clear();
    for (int i = 1; i <= n; i++) {
      if (a[i] > 0) {
        tmp.push_back({a[i], f[i]});
      }
    }
    sort(tmp.begin(), tmp.end());
    reverse(tmp.begin(), tmp.end());
    last = n * 2;
    while (pq.size() > 0) pq.pop();
    cont = 0;
    while (last - 1 > 0 && (cont < tmp.size() || pq.size() > 0)) {
      if (pq.size() == 0) {
        last = min(last - 1, tmp[cont].first);
        pq.push(tmp[cont].second);
        cont++;
      } else {
        last = last - 1;
      }
      while (cont < tmp.size() && tmp[cont].first >= last) {
        pq.push(tmp[cont].second);
        cont++;
      }
      ans1 += last;
      ans2 += min(pq.top(), last);
      pq.pop();
    }
    cout << ans1 << " " << ans2 << "\n";
  }
  return 0;
}
