# 思路：

$$T= \begin{cases} |p|+|q| & \text{pq\textless =0} \\ max(|p|,|q|) & \text{pq\textgreater 0} \end{cases}$$

$$p_0\times a + q_0\times b = Y-X$$

$$\begin{cases} p = p_0 + t\times \frac{b}{gcd(a,b)} \\ q = q_0 – t\times \frac{a}{gcd(a,b)} \end{cases}$$

#include <iostream>
#include <cstdio>
#include <cmath>

#define labs(a) (a>0?a:-a)

using namespace std;

typedef long long ll;

ll eu(ll x,ll y,ll &p,ll &q)
{
if(y==0)
{
p=1,q=0;
return x;
}
int rt=eu(y,x%y,p,q);
p=p-q*(int)(x/y);
swap(p,q);
return rt;
}

ll work(ll x,ll y,ll a,ll b)
{
ll g,p,q,tmp=9999999999999999;
g=eu(a,b,p,q);
if((y-x)%g!=0){return -1;}
ll p1=p*(y-x)/g,q1=q*(y-x)/g;
ll dp=b/g,dq=a/g;
ll tt=(q1-p1)/(dp+dq);
for(int i=tt-1;i<=tt+1;i++)
{
p=p1+i*dp,q=q1-i*dq;
if((p<0&&q>0)||(p>0&&q<0))tmp=min(tmp,labs(p)+labs(q));
else tmp=min(tmp,max(labs(p),labs(q)));
}
return tmp;
}

int main()
{
int t;
ll x,y,a,b;
scanf("%d",&t);
for(int w=1;w<=t;w++)
{
scanf("%lld%lld%lld%lld",&x,&y,&a,&b);
cout<<work(x,y,a,b)<<endl;
}
return 0;
}