#include using namespace std; int main() { int tests; scanf("%d", &tests); for(int qq(1); qq <= tests; qq++) { int n; scanf("%d", &n); vector vec; for(int i(1); i <= n; i++) { int x, y; scanf("%d%d", &x, &y); for(int j(0); j < y; j++) vec.push_back(x); } sort(vec.begin(), vec.end()); vector s, t; s.push_back(0); t.push_back(0); for(int i(0); i < (int)vec.size(); i++) { s.push_back(s.back() + (i + 1) * vec[i]); t.push_back(t.back() + vec[i]); } long long ans(0); for(int i(0); i < (int)vec.size(); i++) { ans = max(ans, s[vec.size()] - s[i] - (t[vec.size()] - t[i]) * i); } cout << ans << endl; } }