#include #include using namespace std; int vtocp[101],vtocn[101]; int n; int main() { int t; ios::sync_with_stdio(false); cin >> t; while(t--) { int v,c; long long sum=0,ans=0; memset(vtocp,0,sizeof(vtocp)); memset(vtocn,0,sizeof(vtocn)); cin >> n; for(int i=1;i<=n;i++) { cin >> v >> c; if(v>=0) vtocp[v]+=c; else vtocn[-v]+=c; } for(int i=100;i>=0;i--) { ans+=sum*vtocp[i]+(long long)i*(vtocp[i]*(vtocp[i]+1)/2); sum+=vtocp[i]*i; } for(int i=1;i<=100;i++) { while(vtocn[i]>0&&sum>=i) { ans+=sum-i; sum-=i; vtocn[i]--; } if(sum