#include <bits/stdc++.h>

using namespace std;

int main()

{

int T;

cin>>T;

while(T–)

{

int N;

cin>>N;

vector<pair<int, int>> vect;

for(int i=0; i<N; i++)

{

int a, b;

cin>>a>>b;

vect.push_back(make_pair(a, b));

}

sort(vect.begin(), vect.end());

int score=0;

for(int i=0; vect[i].first<9;)

{

int max=0;

int term=vect[i].first;

while(vect[i].first==term)

{

if(max<vect[i].second)

max=vect[i].second;

i++;

}

score+=max;

}

cout<<score<<endl;

}

return 0;

}