#include #include #include #include #include #include using namespace std; int main() { int t; scanf("%d",&t); while(t--) { map ha; priority_queue ar[105]; queue haha; int count=1; int ans=0; string temp; int n,tempnum; scanf("%d",&n); while(n--) { cin>>temp; scanf("%d",&tempnum); if(ha[temp]==0) { haha.push(temp); ha[temp]=count++; ar[ha[temp]].push(tempnum); } else { ar[ha[temp]].push(tempnum); } } while(haha.size()) { temp=haha.front(); haha.pop(); if(ar[ha[temp]].size()==1) ans+=ar[ha[temp]].top(); else { int la; la=ar[ha[temp]].top(); ans+=la; ar[ha[temp]].pop(); la=ar[ha[temp]].top(); ans+=la; } } printf("%d\n",ans); } return 0; }