#include #include #include #include #include #include #include #include #include #define pointtype double #define pointtype_INT 0 using namespace std; typedef long long LL; typedef pairpii; typedef unsigned uint; typedef unsigned long long uLL; const double pi=acos(-1); template void Read(T &x){ char c; bool f(0); while(c=getchar(),c!=EOF){ if(c=='-') f=1; else if(c>='0'&&c<='9'){ x=c-'0'; while(c=getchar(),c>='0'&&c<='9') x=x*10+c-'0'; ungetc(c,stdin); if(f) x=-x; return; } } } struct point{ pointtype x,y; inline point(){ } inline point(pointtype x,pointtype y):x(x),y(y){ } inline point operator-(const point &a)const{ return point(x-a.x,y-a.y); } #if pointtype_INT==1 inline point operator*(const point &a)const{ return point(1ll*x*a.x-1ll*y*a.y,1ll*x*a.y+1ll*y*a.x); } inline friend LL cross(const point &a,const point &b){ return 1ll*a.x*b.y-1ll*a.y*b.x; } inline friend LL dot(const point &a,const point &b){ return 1ll*a.x*b.x+1ll*a.y*b.y; } #else inline point operator*(const point &a)const{ return point(x*a.x-y*a.y,x*a.y+y*a.x); } inline friend pointtype cross(const point &a,const point &b){ return a.x*b.y-a.y*b.x; } inline friend pointtype dot(const point &a,const point &b){ return a.x*b.x+a.y*b.y; } #endif }; int T,n; vectora; long long sum,ans; int main() { Read(T); while(T--){ a.clear(); Read(n); int x,y; for(int i=1;i<=n;i++){ sum=ans=0; Read(x),Read(y); while(y--) a.push_back(x); } sort(a.begin(),a.end(),greater()); sum=ans=0; for(auto j:a){ sum+=j; if(sum<=0) break; ans+=sum; } printf("%I64d\n",ans); } }