#include <bits/stdc++.h>
using namespace std;
int n;
int main() {
  ios::sync_with_stdio(0);
  int t;
  cin >> t;
  while (t--) {
    cin >> n;
    vector<int> num(n + 1), num_good(n + 1);
    for (int i = (1); i <= (n); ++i) {
      int aa, xx;
      cin >> aa >> xx;
      num[aa]++;
      if (xx) num_good[aa]++;
    }
    vector<vector<int> > type(n + 1);
    for (int i = (1); i <= (n); ++i) {
      type[num[i]].push_back(num_good[i]);
    }
    int ans1 = 0;
    int ans2 = 0;
    priority_queue<int> q;
    for (int i = (n); i >= (1); --i) {
      for (auto x : type[i]) q.push(x);
      if (!q.empty()) {
        int max_val = q.top();
        q.pop();
        ans1 += i;
        ans2 += min(i, max_val);
      }
    }
    printf("%d %d\n", ans1, ans2);
  }
}
