#include <bits/stdc++.h>
using namespace std;
int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
  int q;
  cin >> q;
  for (int h = 0; h < q; h++) {
    int n, t;
    cin >> n;
    int b[n + 1], a[n], f[n + 1];
    memset(b, 0, sizeof(b));
    memset(f, 0, sizeof(f));
    for (int i = 0; i < n; i++) {
      cin >> a[i] >> t;
      b[a[i]]++;
      if (t) {
        f[a[i]]++;
      }
    }
    vector<pair<int, int> > c;
    for (int i = 0; i < n + 1; i++) {
      if (b[i] > 0) {
        c.push_back(make_pair(b[i], f[i]));
      }
    }
    sort(c.begin(), c.end());
    reverse(c.begin(), c.end());
    long long d = 0;
    int y = c[0].first;
    long long re = 0;
    priority_queue<int> p;
    int i = 0;
    while (y > 0) {
      while (i < c.size() && c[i].first >= y) {
        p.push(c[i].second);
        i++;
      }
      if (p.empty()) {
        y--;
        continue;
      }
      d += y;
      re += min(p.top(), y);
      p.pop();
      y--;
    }
    cout << d << " " << re << "\n";
  }
  return 0;
}
