#include <bits/stdc++.h>
using namespace std;
int main() {
  long long int n, i, j, k, x, y, t, m, cnt;
  cin >> t;
  while (t--) {
    cin >> n;
    vector<pair<long long int, long long int> > V;
    vector<pair<long long int, long long int> > tmp;
    priority_queue<pair<long long int, long long int> > Q;
    for (i = 1; i <= n; i++) {
      cin >> x >> y;
      V.push_back(make_pair(x, y));
    }
    sort(V.begin(), V.end());
    x = V.size();
    i = 0, j = 0;
    while (i < x) {
      j = i;
      long long int f = 0;
      while (j < x) {
        if (V[j].first != V[i].first) break;
        if (V[j].second == 1) f++;
        j++;
      }
      Q.push(make_pair(j - i, f));
      i = j;
    }
    long long int ans = 0, an = 0;
    while (!Q.empty()) {
      pair<long long int, long long int> P = Q.top();
      Q.pop();
      ans += P.first, an += min(P.second, P.first);
      while (!Q.empty() && Q.top().first == P.first) {
        pair<long long int, long long int> T = Q.top();
        Q.pop();
        T.first--;
        if (T.first) Q.push(T);
      }
    }
    cout << ans << " " << an << endl;
  }
}
