#include <bits/stdc++.h>
using namespace std;
vector<long long> v;
vector<pair<long long, long long> > vv;
map<long long, long long> m, mm;
long long n, x, q, y;
int main() {
  cin >> q;
  while (q--) {
    long long sum2 = 0, sum = 0;
    cin >> n;
    for (int i = 0; i < n; i++) {
      cin >> x >> y;
      m[x]++;
      if (y) mm[x]++;
    }
    vector<vector<long long> > vvv(n + 10);
    for (int i = 1; i <= n; i++) vvv[m[i]].push_back(mm[i]);
    multiset<int> st;
    for (int i = n; i > 0; i--) {
      for (auto t : vvv[i]) st.insert(t);
      if (!st.empty()) {
        int z = *st.rbegin();
        sum2 += i;
        sum += min(i, z);
        st.erase(st.find(z));
      }
    }
    mm.clear(), m.clear();
    cout << sum2 << " " << sum << endl;
  }
  return 0;
}
