#include #include #include #include #include #include using namespace std; long long sum[100010]; int main() { int T; scanf("%d", &T); while (T--) { int n, k = 0; scanf("%d", &n); vector v; for (int i = 0; i < n; ++i) { int x, t; scanf("%d%d", &x, &t); k += t; while (t--) { v.push_back(x); } } sort(v.begin(), v.end(), greater()); sum[0] = v[0]; for (int i = 1; i < k; ++i) { sum[i] = sum[i - 1] + v[i]; } long long ans = 0, cur = 0; for (int i = 0; i < k; ++i) { cur += sum[i]; ans = max(ans, cur); } printf("%I64d\n", ans); } return 0; }