#include <bits/stdc++.h>
using namespace std;
int main() {
  ios::sync_with_stdio(false);
  cin.tie(0);
  cout.tie(0);
  int q;
  cin >> q;
  while (q--) {
    int n, a, f;
    cin >> n;
    vector<int> c(n), gc(n);
    for (int i = 0; i < n; ++i) {
      cin >> a >> f;
      a--;
      c[a]++;
      if (f) gc[a]++;
    }
    vector<vector<int> > t(n + 1);
    for (int i = 0; i < n; ++i) {
      t[c[i]].push_back(gc[i]);
    }
    multiset<int> m;
    int a1 = 0, a2 = 0;
    for (int i = n; i > 0; --i) {
      for (int x : t[i]) m.insert(x);
      if (!m.empty()) {
        int x = *m.rbegin();
        a1 += i;
        a2 += min(i, x);
        m.erase(m.find(x));
      }
    }
    cout << a1 << " " << a2 << "\n";
  }
}
