#include <bits/stdc++.h>
#pragma G++ optimize(2)
using namespace std;
inline long long read() {
  char ch = ' ', last;
  long long ans = 0;
  while (ch < '0' || ch > '9') last = ch, ch = getchar();
  while (ch >= '0' && ch <= '9') ans = ans * 10 + int(ch - '0'), ch = getchar();
  if (last == '-') return -ans;
  return ans;
}
void write(long long x) {
  if (x >= 10) write(x / 10);
  putchar(x % 10 + '0');
}
int n, Q, MAX;
pair<int, int> cnt[200105];
priority_queue<int> q;
bool cmp(pair<int, int> i, pair<int, int> j) { return i > j; }
void init() {
  while (!q.empty()) q.pop();
  n = read();
  MAX = 0;
  memset(cnt, false, sizeof(pair<int, int>) * (n + 4));
  for (int i = 1; i <= n; i++) {
    int x = read(), y = read();
    cnt[x].first++, cnt[x].second += y, MAX = max(MAX, x);
  }
  sort(cnt + 1, cnt + MAX + 1, cmp);
}
int main() {
  Q = read();
  while (Q--) {
    init();
    int ans1 = 0, ans2 = 0, now = 0;
    for (int i = cnt[1].first; i >= 1; i--) {
      while (cnt[now + 1].first == i && now + 1 <= MAX)
        q.push(cnt[++now].second);
      if (!q.empty()) ans1 += i, ans2 += min(i, q.top()), q.pop();
    }
    cout << ans1 << ' ' << ans2 << endl;
  }
  return 0;
}
