// ===================================
//   author: M_sea
//   website: http://m-sea-blog.com/
// ===================================
#include <algorithm>
#include <iostream>
#include <cstdlib>
#include <cstring>
#include <cstdio>
#include <vector>
#include <cmath>
#include <queue>
#define re register
using namespace std;
typedef long long ll;

inline int read() {
    int X=0,w=1; char c=getchar();
    while (c<'0'||c>'9') { if (c=='-') w=-1; c=getchar(); }
    while (c>='0'&&c<='9') X=X*10+c-'0',c=getchar();
    return X*w;
}

const int N=200000+10;

int n,m[N],p[N];
vector<int> vec[N];
priority_queue<int,vector<int>,greater<int> > Q;

int main() {
    int T=read();
    while (T--) {
        n=read();
        for (re int i=1;i<=n;++i) vec[i].clear();
        for (re int i=1;i<=n;++i) {
            m[i]=read(),p[i]=read();
            vec[m[i]].push_back(p[i]);
        }
        int pre=n,cnt=0; ll ans=0;
        while (!Q.empty()) Q.pop();
        for (re int i=n-1;~i;--i) {
            pre-=vec[i].size();
            for (re int j:vec[i]) Q.push(j);
            while (cnt+pre<i) ans+=Q.top(),Q.pop(),++cnt;
        }
        printf("%I64d\n",ans);
    }
    return 0;
}
