#include using namespace std; #define N 26010 int t, n, m, val, cnt, a[N], s[N]; int main() { // freopen("a.in", "r", stdin); scanf("%d", &t); while (t --) { scanf("%d", &n); m = 0; for (int i = 1; i <= n; i ++) { scanf("%d %d", &val, &cnt); for (int j = 0; j < cnt; j ++) a[++m] = val; } if (!m) { puts("0"); continue; } sort(a + 1, a + m + 1); s[m+1] = 0; for (int i = m; i >= 1; i --) s[i] = s[i+1] + a[i]; int ans = 0; for (int i = m; i >= 1 && s[i] > 0; i --) ans += s[i]; printf("%d\n", ans); } return 0; }