#include <bits/stdc++.h>
using namespace std;
int main() {
  ios_base::sync_with_stdio(0);
  cin.tie(0);
  cout.tie(0);
  int t, n, p, m, ans;
  map<pair<int, int>, int> ms, ps;
  pair<int, int> _pair;
  cin >> t;
  while (t--) {
    cin >> n;
    ms.clear();
    ps.clear();
    for (int i = 0; i < n; i++) {
      cin >> m >> p;
      pair<int, int> _pair = make_pair(m, p);
      if (ms.find(_pair) == ms.end()) ms.insert(make_pair(_pair, 0));
      ms[_pair]++;
      _pair = make_pair(p, m);
      if (ps.find(_pair) == ps.end()) ps.insert(make_pair(_pair, 0));
      ps[_pair]++;
    }
    int voters = 0;
    long long cost = 0;
    map<pair<int, int>, int>::iterator itm, itp;
    for (itm = ms.begin(); itm != ms.end(); itm++) {
      _pair = itm->first;
      if (itm->second == 0) {
        continue;
      }
      if (voters >= _pair.first) {
        voters += itm->second;
        ms[_pair] = 0;
        ps[make_pair(_pair.second, _pair.first)] = 0;
      } else {
        int diff = _pair.first - voters, curr;
        while (diff) {
          for (itp = ps.begin(); itp != ps.end(); itp++) {
            curr = 0;
            if (itp->second) {
              curr = min(itp->second, diff);
              voters += curr;
              diff -= curr;
              cost += (curr * itp->first.first);
              itp->second -= curr;
              ms[make_pair(itp->first.second, itp->first.first)] -= curr;
            }
            if (!diff) break;
          }
        }
      }
    }
    cout << cost << endl;
  }
  return 0;
}
