#include #include #include using namespace std; struct data{ int coe; int deg; }A[11],B[11],Ans1[20],Ans2[20]; bool cmp(struct data a,struct data b){ if(a.deg>b.deg)return true; else if(a.deg==b.deg){ if(a.deg==0){ if(a.coe!=0)return true; else return false; } else{ if(a.coe>b.coe)return true; else return false; } } else return false; } int main(){ int lines; int a_ptr,b_ptr,tmp_ptr,tmp2_ptr; bool out; cin>>lines; while(lines--){ for(int i=0;i<11;i++){ A[i].coe=0; A[i].deg=0; B[i].coe=0; B[i].deg=0; } for(int i=0;i<20;i++){ Ans1[i].coe=0; Ans1[i].deg=0; Ans2[i].coe=0; Ans2[i].deg=0; } for(int i=0;i<10;i++){ cin>>A[i].coe; if(A[i].coe==0)break; cin>>A[i].deg; } for(int i=0;i<10;i++){ cin>>B[i].coe; if(B[i].coe==0)break; cin>>B[i].deg; } sort(A,A+10,cmp); sort(B,B+10,cmp); a_ptr=b_ptr=tmp_ptr=tmp2_ptr=0; while(true){ if(A[a_ptr].coe==0&&B[b_ptr].coe==0)break; else if(A[a_ptr].deg>B[b_ptr].deg){ Ans1[tmp_ptr++]=A[a_ptr]; Ans2[tmp2_ptr++]=A[a_ptr++]; } else if(B[b_ptr].deg>A[a_ptr].deg){ Ans1[tmp_ptr++]=B[b_ptr]; Ans2[tmp2_ptr++]=B[b_ptr++]; } else{ if(A[a_ptr].coe+B[b_ptr].coe!=0){ Ans1[tmp_ptr].coe=A[a_ptr].coe+B[b_ptr].coe; Ans1[tmp_ptr++].deg=A[a_ptr].deg; } if(A[a_ptr].coe-B[b_ptr].coe!=0){ Ans2[tmp2_ptr].coe=A[a_ptr].coe-B[b_ptr].coe; Ans2[tmp2_ptr++].deg=A[a_ptr].deg; } a_ptr++; b_ptr++; } } out=false; for(int i=0;i<20;i++){ if(Ans1[i].coe==0&&Ans1[i].coe==0)break; if(Ans1[i].deg!=0){ cout<