#include #include #include #include #include #include #include #include #include #include #include #include #include #include using namespace std; #define rep(i,j,k) for (int i=j;i<=k;i++) #define Rrep(i,j,k) for (int i=j;i>=k;i--) #define Clean(x,y) memset(x,y,sizeof(x)) #define LL long long #define ULL unsigned long long #define inf 0x7fffffff #define mod 100000007 int n,N; int main() { std::ios::sync_with_stdio(false); int T; cin>>T; while(T--) { cin>>n; map p; vector a[105]; int ans = 0 , x; string temp; N = 0; rep(i,1,n) { cin>>temp>>x; if ( p[temp] == 0 ) { p[temp] = ++N; } a[ p[temp] ].push_back( x ); } rep(i,1,N) { sort( a[i].begin() , a[i].end() ); ans += a[i][ a[i].size() - 1 ]; if ( a[i].size() > 1 ) ans += a[i][ a[i].size() - 2 ]; } cout<