#include #include #include #include #include #include using namespace std; #define ff first #define ss second #define pb push_back #define ll long long #define mod 1000000007 #define ull unsigned long long #define mst(ss,b) memset(ss,b,sizeof(ss)); #define pl(x) cout << #x << "= " << x << endl; const int inf = 0x3f3f3f3f; const int N = 1e5+5; mapmp; vectorve[105]; int main(){ ios::sync_with_stdio(false); int T; cin>>T; while(T--){ mp.clear(); int n; cin>>n; for(int i=1; i<=n; i++)ve[i].clear(); string s; int v, tol = 0; for(int i=1; i<=n; i++){ cin>>s>>v; if(!mp[s])mp[s] = ++tol; ve[mp[s]].pb(v); } for(int i=1; i<=tol; i++)sort(ve[i].begin(), ve[i].end()); int ans = 0; for(int i=1; i<=tol; i++){ int mx = 0, mm = 0; for(auto x : ve[i]){ if(x>mx){ mm =max(mx, mm); mx = x; } else if(x > mm && x <= mx)mm = x; } ans += (mx + mm); } cout<