The meaning of problems
Some people say to convince
everyone can use the money to buy, or to reach persuaded
Human condition
answer
We can consider
, when only one person can reach a condition, then I must leave
biggest.
So, when
When able to reach conditions may have
personal.
In order to meet all the inequality, we pushed backwards, and more than once
We removed from
The minimum buy.
#include<bits/stdc++.h>
#define FOR(i,l,r) for(int i=l;i<=r;i++)
#define sf(x) scanf("%d",&x)
typedef long long ll;
using namespace std;
const ll mod = 1e9+7;
const int maxn = 500050;
vector<int>G[maxn];
int n,p[maxn],m[maxn];
int main(){
int T;cin>>T;
while(T--){
scanf("%d",&n);
FOR(i,1,n)scanf("%d%d",&m[i],&p[i]);
FOR(i,0,n-1)G[i].clear();
FOR(i,1,n)G[m[i]].push_back(p[i]);
priority_queue<int,vector<int>,greater<int> >q;
ll ans=0;
for(int i=n-1;i>=0;i--){
for(auto x:G[i])q.push(x);
while(q.size()>n-i){
ans+=q.top();
q.pop();
}
//cout<<q.size()<<endl;
}
printf("%lld\n",ans);
}
}