#include typedef std::pair pa; std::map mp; std::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++) { std::cin >> s >> v; if (mp.find(s) == mp.end()) ans += v, mp[s] = std::make_pair(v, 0); else if (mp[s].first < v) ans += v - mp[s].second, mp[s] = std::make_pair(v, mp[s].first); else if (mp[s].second < v) ans += v - mp[s].second, mp[s] = std::make_pair(mp[s].first, v); } std::cout << ans << std::endl; } return 0; }