#include <bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp>

using namespace __gnu_pbds;
using namespace std;

typedef tree<int,null_type,less<int>,rb_tree_tag, tree_order_statistics_node_update> indexed_set;

typedef pair<int, int> pii;
typedef pair<double, double> pdd;
typedef long long ll;
typedef pair<ll, ll> pll;

mt19937 rng(chrono::high_resolution_clock::now().time_since_epoch().count());

pll a[200000];

int main()
{
    ios_base::sync_with_stdio(0), cin.tie(0), cout.tie(0);
    int t; cin>>t;
    while(t--){
        int n; cin>>n;

        pll minas = {LLONG_MAX, LLONG_MAX};
        for(int i=0; i<n; i++){
            cin>>a[i].first>>a[i].second;
            minas = min(minas, {a[i].first, -a[i].second});

        }
        priority_queue<ll, vector<ll>, greater<ll>> pq;
        for(int i=0; i<n; i++){
            if(make_pair(a[i].first, -a[i].second)==minas)continue;
            pq.push(a[i].second);
        }
        ll ans = 0;
        while(minas.first--){
            ans+=pq.top();
            pq.pop();
        }
        cout<<ans<<'\n';
    }



    return 0;
}
