#include #include #include #include #include #include #include #include using namespace std; #define sf scanf #define pf printf #define pb push_back #define fo(i,s,t) for(int i = s; i <= t; ++ i) #define fd(i,s,t) for(int i = s; i >= t; -- i) #define go(i,s) for(int i = head[s]; i; i = e[i].next) const int maxn = 21; typedef long long ll; int t, n; map hs1; vector g[105]; bool cmp(int a,int b){return a>b;} void solve() { string s; int x, t, ans = 0; t = 0; sf("%d",&n); hs1.clear(); fo(i,1,n) { cin >> s >> x; if(!hs1[s]) { hs1[s] = ++t; g[t].clear(); } g[hs1[s]].pb(x); } fo(i,1,t) sort(g[i].begin(),g[i].end(),cmp); fo(i,1,t) { if(g[i].size()>=2) ans += g[i][0]+g[i][1]; if(g[i].size()==1) ans += g[i][0]; } pf("%d\n",ans); } int main() { sf("%d",&t); while(t--){solve();} return 0; }