显示代码纯文本
#include<bits/stdc++.h>
#define ll long long
using namespace std;
const int N=110;
const int M=2010;
const int MOD=998244353;
int T;
int main()
{
freopen("Transforming.in","r",stdin);
freopen("Transforming.out","w",stdout);
cin>>T;
while(T)
{
T--;
ll a,b,c,d;
cin>>a>>b>>c>>d;
ll ans=0;
if(c<a||d<b){
cout<<-1<<endl;
continue;
}else if(c==a&&d==b){
cout<<0<<endl;
continue;
}
bool flag=true;
while(c!=a||d!=b){
if(c<a||d<b){
cout<<-1<<endl;
flag=false;
break;
}
if(c>=d){
ll t=(c-a)/d;
ans+=max(t,1ll);
c-=max(t,1ll)*d;
}else if(c<d){
ll t=(d-b)/c;
ans+=max(t,1ll);
d-=max(t,1ll)*c;
}
}
if(flag) cout<<ans<<endl;
}
return 0;
}