#include #include #include #include #include #include #include #include #include #include #include #include #include #define INF 0X3F3F3F3F #define N 100010 #define M 2000100 #define LL long long #define FF(i,a,b) for(int i=a;i<=b;++i) #define RR(i,a,b) for(int i=a;i>=b;--i) #define FJ(i,a,b) for(int i=a;i>1) #define lson rt<<1,l,mid #define rson rt<<1|1,mid+1,r #define zero(x) (((x)>0?(x):-(x)) #define PLL pair #define MP make_pair #define IN freopen("in.txt","r",stdin) #define OUT freopen("out.txt","w",stdout) using namespace std; const int MOD=1000000007; const double PI=acos(-1.0); const double EPS=1e-8; //inline int II(){int n=0,ch=getchar();bool t=0;if(ch==-1)return 0; //for(;ch<'0'||ch>'9';ch=getchar())if(ch=='-')t=1; //for(;ch>='0'&&ch<='9';n=n*10+ch-'0',ch=getchar());if(t)return -n;return n;} //inline void OO(int a){if(a<0){putchar('-');a=-a;} //if(a>=10)OO(a/10);putchar(a%10+'0');} //inline int sgn(double x){return(x>EPS)-(x<-EPS);} //struct edge{int v,w,nx;}E[M*2+N*2];int head[N],EN; //void init_edge(){EN=0;CL(head,-1);} //void add_edge(int u,int v,int w){ // E[EN]=(edge){v,w,head[u]};head[u]=EN++; // E[EN]=(edge){u,w,head[v]};head[v]=EN++; //} int ha[6000],hanum; int f[6000],fnum; int sum,n,x,y; LL ans; int main(){ // IN; int _; for(SC(_);_--;){ sum=hanum=fnum=0; ans=0;SC(n); FF(i,1,n){ SCC(x,y); if(x>0)sum+=x*y; if(x>=0){ FF(j,1,y){ f[++fnum]=x; } } if(x<0){ FF(j,1,y){ ha[++hanum]=-x; } } } sort(ha+1,ha+1+hanum); sort(f+1,f+1+fnum); FF(i,1,fnum){ ans+=1LL*i*f[i]; } int now=1; int tmp=0; for(;now<=hanum;++now){ if((tmp+ha[now])