#include #define mem(a, b) memset(a, b, sizeof(a)) using namespace std; map mps; string str; int n, num[105][105], nn; int main() { int cas; scanf("%d", &cas); while(cas--) { nn = 0; mps.clear(); scanf("%d", &n); for(int i = 1, a;i <= n;i++) { cin>>str>>a; if(!mps[str]) mps[str] = ++nn, num[nn][0] = 0; int tmp = mps[str]; num[tmp][++num[tmp][0]] = a; } for(int i = 1;i <= nn;i++) sort(num[i] + 1, num[i] + 1 + num[i][0]); int ans = 0; for(int i = 1;i <= nn;i++) { for(int j = num[i][0];j >= max(num[i][0] - 1, 1);j--) { ans += num[i][j]; } } printf("%d\n", ans); } return 0; }