#include #include #include #include using namespace std; ///---------------------------------------------- const int P_N = 7 + 100; const int MAX_N = 7 + 100; char str[MAX_N][20]; struct P{ int str,val; } p[P_N]; bool operator <(const P&A,const P&B) { return strcmp(str[A.str],str[B.str])==0 ? A.val>B.val : strcmp(str[A.str],str[B.str])<0; } ///---------------------------------------------- int main() { int tcase=0; int icase=0; for (scanf("%d",&tcase); ++icase<=tcase; ){ ///init ///read int n; scanf("%d",&n); for (int i=1; i<=n; i++) { scanf(" %s%d",str[i],&p[i].val); p[i].str=i; } ///prework sort(p+1,p+1+n); ///work int tim=1; int ans=p[1].val; for (int i=2; i<=n; i++) { if (strcmp(str[p[i].str],str[p[i-1].str])==0) tim++; else tim=1; if (tim<=2) ans+=p[i].val; } ///print printf("%d\n",ans); } }