#include #include #include #include #include #include #include #include #include #include using namespace std; typedef long long ll; typedef unsigned long long ull; typedef pair PII; #define fi first #define se second #define MP make_pair inline int read() { int v = 0, f = 1; char c = getchar(); while (c < 48 || 57 < c) {if (c == '-') f = -1; c = getchar();} while (48 <= c && c <= 57) v = v * 10 + c - 48, c = getchar(); return v * f; } typedef map > P; P mp; int main() { int T = read(); string s; while (T--) { int n = read(); mp.clear(); int ans = 0; while (n--) { cin >> s; mp[s].push_back(read()); } for (P::iterator i = mp.begin(); i != mp.end(); i++) { sort(((*i).second).begin(), ((*i).second).end()); if (((*i).second).size() >= 2) { int t = ((*i).second).size(); ans += ((*i).second)[t - 1]; ans += ((*i).second)[t - 2]; } else { int t = ((*i).second).size(); ans += ((*i).second)[t - 1]; } } printf("%d\n", ans); } }