ll@t; rep(t){ ll@d,@x,@y; if(x*y==0){ wt((x+y)*d); }else{ ll g=gcd(x,y); ll u=y/g; ll v=x/g; ll a=min(x/u,(d-y)/v); ll b=min((d-x)/u,y/v); wt(max(a,b)*(v*x+u*y)); } }