#include <bits/stdc++.h>
using namespace std;
int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(0);
  int t;
  cin >> t;
  while (t--) {
    int n;
    cin >> n;
    pair<long long, long long> arr[n];
    for (int i = 0; i < n; i++) cin >> arr[i].first >> arr[i].second;
    sort(arr, arr + n);
    vector<pair<long long, long long> > taken;
    int cur = 0;
    for (int i = 0; i < n; i++) {
      pair<long long, long long> x = arr[i];
      if (x.first <= i) {
        long long maxV = 0;
        for (int j = 0; j < (long long)taken.size(); j++) {
          maxV = max(maxV, taken[j].second);
        }
        if (x.second < maxV) {
          for (int j = 0; j < (long long)taken.size(); j++) {
            if (taken[j].second == maxV) {
              taken[j] = x;
              break;
            }
          }
        }
      } else {
        long long maxV = 0;
        bool replaced = false;
        for (int j = 0; j < (long long)taken.size(); j++) {
          if (taken[j].first <= i) maxV = max(maxV, taken[j].second);
        }
        for (int j = 0; j < (long long)taken.size(); j++) {
          if (taken[j].second == maxV && taken[j].first <= i) {
            taken[j] = x;
            replaced = true;
            break;
          }
        }
        if (!replaced) {
          taken.push_back(x);
        }
      }
    }
    long long ans = 0;
    for (pair<long long, long long> x : taken) ans += x.second;
    cout << ans << "\n";
  }
}
