#include #include #include #include #include #include #include #include #include #define debug(x) cerr << #x << "=" << x << endl #define LL long long #define MAXN 1000010 using namespace std; int read(){ int x = 0, f = 1; char ch = getchar(); for(; !isdigit(ch); ch = getchar()) if(ch == '-') f = -1; for(; isdigit(ch); ch = getchar()) x = x * 10 + ch - '0'; return x * f; } const int inf = 1e7; int T, n; struct Note{int v, c; } a[30]; int b[MAXN]; int main(){ T = read(); while(T--){ n = read(); for(int i = 1; i <= n; i++) a[i].v = read(), a[i].c = read(); int cnt = 0; for(int i = 1; i <= n; i++) for(int j = 1; j <= a[i].c; j++) b[++cnt] = a[i].v; sort(b + 1, b + cnt + 1); int t = 1; while(b[t] < 0) t++; int sum = 0, ans = -inf, now = 0; for(int i = t; i <= cnt; i++) now += b[i] * (i - t + 1), sum += b[i]; ans = max(ans, now); for(int i = t - 1; i; i--) now = now + sum + b[i], sum += b[i], ans = max(now, ans); printf("%d\n", ans); } return 0; }