#include <bits/stdc++.h>
using namespace std;
using ll = long long;
int t, n;
bool vis[200005];
pair<ll, int> v[200005];
const double INF = 1000000009;
int main() {
  cin >> t;
  int tot = 0;
  for (int i = 0; i < t; ++i) {
    cin >> n;
    ll l = 0, r = 0;
    set<pair<int, int>> ms;
    set<pair<long double, int>> vs;
    for (int j = tot + 0; j < tot + n; ++j) {
      ll m, p;
      cin >> m >> p;
      v[j] = make_pair(m, p);
      ms.insert(make_pair(m, j));
      if (p == 0) {
        vs.insert(make_pair(INF, j));
      } else {
        vs.insert(make_pair(-(long double)m / (long double)p, j));
      }
      r += p;
    }
    if (i == 10) {
      cout << "n is " << n << endl;
      for (int k = tot; k < tot + n; ++k) {
        cout << v[k].first << " " << v[k].second << endl;
      }
    }
    while (l < r) {
      ll mid = (l + r) >> 1;
      int cnt = 0;
      ll val = mid;
      for (auto& vp : vs) {
        if (val >= v[vp.second].second) {
          cnt++;
          vis[vp.second] = true;
          val -= v[vp.second].second;
        } else {
          vis[vp.second] = false;
        }
      }
      for (auto& mp : ms) {
        if (cnt >= mp.first && !vis[mp.second]) {
          cnt++;
          vis[mp.second] = true;
        }
      }
      if (cnt == n) {
        r = mid;
      } else {
        l = mid + 1;
      }
    }
    cout << l << endl;
    tot += n;
  }
  return 0;
}
