#include #include #include #include #include #include #include #include #include #include #include #include #define X first #define Y second #define clr(u,v); memset(u,v,sizeof(u)); #define in() freopen("data","r",stdin); #define out() freopen("ans","w",stdout); #define Clear(Q); while (!Q.empty()) Q.pop(); #define pb push_back using namespace std; typedef long long ll; typedef pair pii; const int maxn = 1e5 + 10; const int INF = 0x3f3f3f3f; pii N[100]; vector V; int sum[maxn]; int main() { int T; scanf("%d", &T); while (T--) { V.clear(); int n; scanf("%d", &n); for (int i = 0; i < n; i++) { scanf("%d%d", &N[i].X, &N[i].Y); while (N[i].Y--) V.pb(N[i].X); } sort(V.begin(), V.end()); sum[V.size()] = 0; for (int i = V.size() - 1; i >= 0; i--) sum[i] = sum[i+1] + V[i]; ll ans = 0, k = 1; //puts(""); for (int i = 0; i < V.size(); i++) if (V[i] >= 0 || V[i] + sum[i+1] >= 0) { //cout<