#include #include #include #include #include #include #include #include using namespace std; #define PI acos(-1.0) #define eps 1e-8 #define ll long long #define MEM(a, b) memset(a, b, sizeof(a)) #define pb push_back #define mp make_pair #define MII map::iterator #define MLL map::iterator #define pii pair #define SI set::iterator #define SL set::iterator #define bug printf("bug-------bug-------bug\n") int a[105][2]; int main() { int T, n; cin >> T; while(T--) { cin >> n; string s; int val, index; memset(a, -1, sizeof(a)); set st; map mp; for(int i = 1; i <= n; i++) { cin >> s >> val; if(st.find(s) != st.end()) { index = mp[s]; } else { mp[s] = st.size();; index = st.size(); st.insert(s); } if(a[index][0] == -1 && a[index][1] == -1) a[index][0] = val; else if(a[index][0] != -1 && a[index][1] == -1) { a[index][1] = val; if(a[index][1] > a[index][0]) swap(a[index][1], a[index][0]); } else { if(val > a[index][0]) { a[index][1] = a[index][0]; a[index][0] = val; } else if(val > a[index][1]) a[index][1] = val; } } int ans = 0; for(int i = 0; i < st.size(); i++) { if(a[i][0] != -1) { ans += a[i][0]; //cout << a[i][0] << endl; } if(a[i][1] != -1) { ans += a[i][1]; //cout << a[i][1] << endl; } } cout << ans << endl; } return 0; }