显示代码纯文本
#include<bits/stdc++.h>
#define ll long long
using namespace std;
ll t,a,b,c,d;
ll f(ll a,ll b,ll c,ll d){
ll ans=0;
if(c<a||d<b) return -1;
if(a==c&&b==d) return 0;
while(c>a||d>b){
if(c>d){
if(d==0) return -1;
ll k=(c-a)/d;
if(k==0) return -1;
c-=k*d;
ans+=k;
}else if(d>c){
if(c==0) return -1;
ll k=(d-b)/c;
if(k==0) return -1;
d-=k*c;
ans+=k;
}else return -1;
}
if(a==c&&b==d) return ans;
return -1;
}
int main(){
freopen("Transforming.in","r",stdin);
freopen("Transforming.out","w",stdout);
cin>>t;
while(t--){
cin>>a>>b>>c>>d;
cout<<f(a,b,c,d)<<endl;
}
return 0;
}