#include #include #include #include #include #include #include #include #include #include #include #include #define ll long long #define pb push_back #define INF 0x3f3f3f3f #define lson l, mid, rt<<1 #define rson mid+1, r, rt<<1|1 using namespace std; const int maxn = 1e2 + 5; const double eps = 1e-7; int t, n; int cnt = 0; struct node { int a = 0; int b = 0; void add(int i) { if (i > a) { int tmp = a; a = i; b = tmp; } else if (i > b) { b = i; } } } v[maxn]; int main() { string tmp; cin >> t; while (t--) { cin >> n; map m; cnt = 0; for (int i = 0; i < n; i++) v[i].a = v[i].b = 0; for (int i = 0; i < n; i++) { cin >> tmp; if (m.find(tmp) != m.end()) { int vv; cin >> vv; v[m[tmp]].add(vv); } else { m[tmp] = cnt; int vv; cin >> vv; v[cnt].add(vv); cnt++; } } int ans = 0; for (int i = 0; i < cnt; i++) { //cout << v[i].a << " " << v[i].b << endl; ans += v[i].a; ans += v[i].b; } cout << ans << endl; } return 0; }