////#define DEBUG
#include <bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp> 
#include <ext/pb_ds/tree_policy.hpp> 
using namespace __gnu_pbds; 
using namespace std;

#define BSET(a, p) ((a) | (1ll << (p)))
#define BCHK(a, p) ((a) & (1ll << (p)))
#define BXOR(a, p) ((a) ^ (1ll << (p)));
#define BREM(a, p) (BCHK(a, p)?(BXOR(a, p)):(a))
#define BSHO(a, N) (bitset<N>(a))

#define m first
#define p second 
#define pb push_back


#ifdef DEBUG
    #define dbg(s) {s;}
    #define PRELUDE
#endif

#ifndef DEBUG 
    #define PRELUDE { ios :: sync_with_stdio(false); cin.tie(0); cout.tie(0); }
    #define dbg(s)
    #define endl "\n"
#endif

using namespace std;


#define int ll
#define i32 int32_t

#define RBTTYPE int
#define ordered_set tree<RBTTYPE, null_type, less<RBTTYPE>, rb_tree_tag,tree_order_statistics_node_update> 

#define all(v) (v).begin(),(v).end()

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

mt19937 rng(chrono::steady_clock::now().time_since_epoch().count());
inline int ri(int x) { // from [0, n-1]
    return uniform_int_distribution<int>(0, x - 1)(rng);
}

inline ld rf() { // from [0, 1]
    return uniform_real_distribution<ld>(0, 1)(rng);
}

ll gcd(ll x, ll y) {
    if (x < y) return gcd(y, x);
    if (y == 0) return x;
    return gcd(y, x % y);
} 

const ll mod = 1e9 + 7;

ll modexp(ll x, ll ex) {
    ll ans = 1ll;
    while (ex > 0) {
        if (ex & 1ll) ans = (ans * x) % mod;
        ex >>= 1ll;
        x = (x * x) % mod;
    }
    return ans;
}

const int maxn = 1e6 + 7;

const ll inf = 1e9 + 7;

struct node {
    node *lf, *rg;
    int sum; int cnt;
    node() : lf(NULL), rg(NULL), sum(0ll), cnt(0ll) {}
};

struct DynamicSegtree {

    node* root;
    
    void build() {
        clean(root);
        root = new node();
    }

    void ins_(node* x, int l, int r, int v) {
        
        x->cnt++; x->sum += v;
       
        if (l == r) return;
        
        int m = (l + r) / 2;
       
        if (v <= m) {
            if (!x -> lf) x -> lf = new node();
            ins_(x -> lf, l, m, v);
        } else {
            if (!x -> rg) x -> rg = new node();
            ins_(x -> rg, m + 1, r, v);
        }
    }
    
    pii smsum_(node* x, int k) {
        
        if (k == 0) return {0, 0};
        
        if (x -> cnt <= k) {
            return {x -> sum, x -> cnt};
        }
        
        int lc = (x -> lf) ? (x -> lf -> cnt) : 0;
        int ls = (x -> lf) ? (x -> lf -> sum) : 0;
        
        if (lc >= k) return smsum_(x -> lf, k);

        //assert(x -> rg); assert(x -> rg -> cnt >= k - lc);
        int total, rgc; tie(total, rgc) = smsum_(x -> rg, k - lc);
        return {total + ls, k};
    }

    void remv_(node* x, int k) {
        if (!x) return;
        if (k == 0) return;
        if (x -> cnt <= k) {
            if (x -> lf) free(x -> lf);
            if (x -> rg) free(x -> rg);
            x -> lf = NULL; x -> rg = NULL;
            x -> cnt = 0; x -> sum = 0;
            return;
        }
        int c = (x -> lf) ? (x -> lf -> cnt) : 0;
        if (x -> lf) remv_(x -> lf, k);
        int rem = (x -> lf) ? (k - c) : k;
        remv_(x -> rg, rem);
    }
    
    void ins(int x) {
        ins_(root, 1, inf, x);
    }

    pii qry(int k) {
        return smsum_(root, k);
    }

    void remv(int k) {
        remv_(root, k);
    }

    void clean(node* x) {
        if (!x) return;
        if (x -> lf) clean(x -> lf);
        if (x -> rg) clean(x -> rg);
        delete x;
    }

} DS;

pii a[maxn];
int suf[maxn];

void solve() {
    int n; cin >> n;
    for (int j = 1; j <= n; j++) {
        cin >> a[j].m >> a[j].p;
    }
    
    sort(a + 1, a + n + 1);
    
    DS.build();
    int sel = 0, ans = 0;

    for (int j = n; j >= 1; ) {
        int i = j + 1;
        
        while (i - 1 >= 1 && a[i - 1].m == a[j].m) {
            i--; DS.ins(a[i].p);
        }
    
        //cout << i << " .. " << j << " = " << sel << endl;

        if (sel + i - 1 < a[j].m) {
            int q = a[j].m - (sel + i - 1);
            int total, cnt; tie(total, cnt) = DS.qry(q);
            
            //assert(cnt >= q); 
            ans += total;
            
            DS.remv(q); sel += q;
        }
        j = i - 1;
    }

    cout << ans << endl;
}

i32 main() {
    //freopen("<file>.in", "r", stdin);
    //freopen("<file>.out", "w", stdout);
    
    PRELUDE;
    
    int t; cin >> t;
    while (t--) solve();
}



