#include #include #include #include #include using namespace std; struct lesson { char ch[15]; int v; }node[110]; bool cmp(lesson a, lesson b) { return a.v < b.v; } int main() { //freopen("data.in", "r", stdin); int T; scanf("%d", &T); while(T--){ int n, sum=0; scanf("%d", &n); for(int i=1; i<=n; i++){ scanf("%s%d", node[i].ch, &node[i].v); } sort(node+1, node+1+n, cmp); map point; for(int i=n; i>=1; i--){ if(!point[node[i].ch]){ sum+=node[i].v; point[node[i].ch]=1; } else{ point[node[i].ch]++; if(point[node[i].ch] > 2) continue; else sum+=node[i].v; } } printf("%d\n", sum); } }