#include <bits/stdc++.h>
using namespace std;
const int maxN = 2 * 1e5 + 2;
int src[maxN], f[maxN];
pair<int, int> a[maxN];
int main() {
  int q;
  cin >> q;
  while (q--) {
    int n;
    cin >> n;
    for (int i = 0; i < n; i++) {
      int temp1, temp2;
      cin >> temp1 >> temp2;
      temp1--;
      src[temp1] += 1;
      f[temp1] += temp2;
    }
    int j = 0;
    for (int i = 0; i < n; i++)
      if (src[i]) {
        a[j] = make_pair(src[i], f[i]);
        src[i] = 0;
        f[i] = 0;
        j++;
      }
    sort(a, a + j);
    pair<int, int> ans = make_pair(0, 0);
    j--;
    priority_queue<int> pq;
    for (int i = a[j].first; i > -1; i--) {
      while (a[j].first >= i && j > -1) {
        pq.push(a[j].second);
        j--;
      }
      if (!pq.empty()) {
        ans.second += min(pq.top(), i);
        ans.first += i;
        pq.pop();
      }
    }
    cout << ans.first << " " << ans.second << endl;
  }
  return 0;
}
