# acm-(辗转相除法、丢番图方程)2020 China Collegiate Programming Contest Qinhuangdao Site I. Interstellar Hunter

{ e 1 ( x 1 , y 1 ) + e 2 ( x 2 , y 2 ) = ( d , e 1 y 1 + e 2 y 2 ) x 2 d ( x 1 , y 1 ) − x 1 d ( x 2 , y 2 ) = ( 0 , x 2 d y 1 − x 1 d y 2 ) ⇒ { ( x 1 , y 1 ) = x 1 d ( d , e 1 y 1 + e 2 y 2 ) + e 2 ( 0 , x 2 d y 1 − x 1 d y 2 ) ( x 2 , y 2 ) = x 2 d ( d , e 1 y 1 + e 2 y 2 ) − e 1 ( 0 , x 2 d y 1 − x 1 d y 2 ) \begin{cases}e_1(x_1,y_1)+e_2(x_2,y_2)=(d,e_1y_1+e_2y_2)\\\frac {x_2}d(x_1,y_1)-\frac{x_1}d(x_2,y_2)=(0,\frac{x_2}dy_1-\frac{x_1}dy_2) \end{cases}\Rightarrow\begin{cases}(x_1,y_1)=\frac{x_1}d(d,e_1y_1+e_2y_2)+e_2(0,\frac{x_2}dy_1-\frac{x_1}dy_2) \\(x_2,y_2)=\frac{x_2}d(d,e_1y_1+e_2y_2)-e_1(0,\frac{x_2}dy_1-\frac{x_1}dy_2)\end{cases}

struct Node{
ll x,y;
};
void merge(Node &a,Node &b){
if(!a.x && !b.x){
a.y=__gcd(a.y,b.y);
b.x=b.y=0;
}else if(!a.y && !b.y){
a.x=__gcd(a.x,b.x);
b.x=b.y=0;
}
while(b.x){
ll t=a.x/b.x;
a.x-=t*b.x,a.y-=t*b.y;
swap(a,b);
}
b.y=abs(b.y);
if(a.x<0)a.x*=-1,a.y*=-1;
if(b.y)a.y=(a.y%b.y+b.y)%b.y;
}
bool check(Node a,Node b,Node c){
if(a.x){
if(c.x%a.x)return false;
ll k=c.x/a.x;
c.y-=a.y*k;
}else if(c.x)return false;
if(!b.y)return !c.y;
return !(c.y%b.y);
}
int main(){
int t=rd(),kase=0;
while(t--){
int q=rd();
ll ans=0;
Node a={0,0},b={0,0};
while(q--){
int op=rd(),x=rd(),y=rd(),w=0;
if(op==1){
Node c={x,y};
merge(a,c);
merge(b,c);
merge(a,b);
}else{
w=rd();
Node c={x,y};
if(check(a,b,c))ans+=w;
}
}
printf("Case #%d: %lld\n",++kase,ans);
}
}