#include using namespace std; typedef pair < int, int > pa; map < string, pa > mp; string s; int main() { int T, n; scanf( "%d", &T ); while( T-- ) { mp.clear(); int ans = 0, v; scanf( "%d", &n ); for( int i = 1 ; i <= n ; i++ ) { cin >> s >> v; if( mp.find( s ) == mp.end() ) ans += v, mp[ s ] = make_pair( v, 0 ); else if( mp[ s ].first < v ) ans += v - mp[ s ].second, mp[ s ] = make_pair( v, mp[ s ].first ); else if( mp[ s ].second < v ) ans += v - mp[ s ].second, mp[ s ] = make_pair( mp[ s ].first, v ); } cout << ans << endl; } }