#include <bits/stdc++.h>
using namespace std;

/**************TEMPLATE****************************************/
#define ll long long
#define ld long double
#define M_PI           3.14159265358979323846
#define F first
#define S second
#define mod 1000000007
#define len length()
#define pb push_back
#define input(a,n)      for(long long int cx=0;cx<n;cin>>a[cx++]);
#define out(a,n)      for(long long int cx=0;cx<n;cout<<a[cx++]<<" ");
#define it iterator
#define io ios_base::sync_with_stdio(false);cin.tie(NULL);cout.tie(NULL);

typedef vector <ll> vll;
typedef vector <int> vi;
typedef pair<ll,ll> pll;
typedef map<ll,ll> mll;
typedef vector <pair<ll,ll> > vpll;

// // #define output(vec,x)   for(long long int c=0;c<a.size();x<<a[c++]<<',');x<<endl;
/**************************************************************/

ll fact(ll num)
{
    ll x=1;
    for (ll i = 1; i <= num; ++i)
    {
        x=x*i;
    }
    return x;
}

void swap(ll *a, ll *b)
{
    ll x=*b;
    *b=*a;
    *a=x;
}

int isPrime(ll num)
{
    if(num <= 1) return 0;
    for (ll i = 2; i * i <= num; i++)
        if (num % i == 0) return 0;
    return 1;
}

long long binomialCoeff(long long n, long long k)
{
    long long C[k+1];
    memset(C, 0, sizeof(C));

    C[0] = 1;  // nC0 is 1

    for (long long i = 1; i <= n; i++)
    {
        for (long long j = min(i, k); j > 0; j--)
            C[j] = (C[j] + C[j-1]) % mod;
    }
    return C[k];
}

ll power(ll x, ll y, ll p)
{
    // cout<<"hi"<<endl;
    ll res = 1;

    x = x % p;

    while (y > 0)
    {
        if (y & 1)
            res = (res*x) % p;

        y = y>>1;
        x = (x*x) % p;
    }
    return res;
}

ll modFact(ll n, ll p)
{
    if (n >= p)
        return 0;

    ll result = 1;
    for (ll i = 1; i <= n; i++)
        result = (result * i) % p;

    return result;
}

ll binarySearch(ll arr[], ll l, ll r, ll x)
{
    if (r >= l) {
        ll mid = l + (r - l) / 2;

        if (arr[mid] == x)
            return mid;

        if (arr[mid] > x)
            return binarySearch(arr, l, mid - 1, x);

        return binarySearch(arr, mid + 1, r, x);
    }
    return -1;
}

ll decrease(int x,int y,int p ,int q)
{
    ll low = 1;
    ll high = 1e9;

    ll res = -1;

    while(low<=high)
    {
        ll mid = (low+high)/2;

        if((long double)x/(y+mid) <= (long double)p/q)
        {
            res = mid;
            high = mid-1;
        }
        else
        {
            low = mid+1;
        }
    }

    return res;
}

ll increase(int x,int y,int p ,int q)
{
    ll low = 1;
    ll high = 1e9;

    ll res = -1;

    while(low<=high)
    {
        ll mid = (low+high)/2;
// cout<<low<<" "<<high<<" "<<mid<<endl;
        if((long double)(x+mid)/(y+mid) >= (long double)p/q)
        {
            res = mid;
            high = mid-1;
        }
        else
        {
            low = mid+1;
        }

        // cout<<res<<endl;
    }

    return res;
}

int main() {
    io;
    ll maxm=0,ans=0,sum=0,count=0,minm=1000000000000;

    ll m,n,k,t,x,y,p,q;
    ll f=0;

    cin>>t;
    while(t--)
    {
        cin>>x>>y>>p>>q;

        if((long double)x/y == (long double)p/q)
        {
            cout<<0<<endl;
            continue;
        }

        if((long double)p/q==0 || (long double)p/q==1)
        {
            cout<<-1<<endl;
            continue;
        }

        ll x1= x, y1=y;

        while((long double)x/y != (long double)p/q)
        {
            if((long double)x/y > (long double)p/q)
            {
                ll d = decrease(x,y,p,q);
                y+=d;
            }
            else
            {
                ll d = increase(x,y,p,q);
                x+=d;y+=d;
            }
        }
    // cout<<x<<" "<<y<<endl;
        cout<<y-y1<<endl;
    }

    return 0;
}

// g++ -std=c++14 code.cpp


#include <bits/stdc++.h>
using namespace std;

/**************TEMPLATE****************************************/
#define ll long long
#define ld long double
#define M_PI           3.14159265358979323846
#define F first
#define S second
#define mod 1000000007
#define len length()
#define pb push_back
#define input(a,n)      for(long long int cx=0;cx<n;cin>>a[cx++]);
#define out(a,n)      for(long long int cx=0;cx<n;cout<<a[cx++]<<" ");
#define it iterator
#define io ios_base::sync_with_stdio(false);cin.tie(NULL);cout.tie(NULL);

typedef vector <ll> vll;
typedef vector <int> vi;
typedef pair<ll,ll> pll;
typedef map<ll,ll> mll;
typedef vector <pair<ll,ll> > vpll;

// // #define output(vec,x)   for(long long int c=0;c<a.size();x<<a[c++]<<',');x<<endl;
/**************************************************************/

ll fact(ll num)
{
    ll x=1;
    for (ll i = 1; i <= num; ++i)
    {
        x=x*i;
    }
    return x;
}

void swap(ll *a, ll *b)
{
    ll x=*b;
    *b=*a;
    *a=x;
}

int isPrime(ll num)
{
    if(num <= 1) return 0;
    for (ll i = 2; i * i <= num; i++)
        if (num % i == 0) return 0;
    return 1;
}

long long binomialCoeff(long long n, long long k)
{
    long long C[k+1];
    memset(C, 0, sizeof(C));

    C[0] = 1;  // nC0 is 1

    for (long long i = 1; i <= n; i++)
    {
        for (long long j = min(i, k); j > 0; j--)
            C[j] = (C[j] + C[j-1]) % mod;
    }
    return C[k];
}

ll power(ll x, ll y, ll p)
{
    // cout<<"hi"<<endl;
    ll res = 1;

    x = x % p;

    while (y > 0)
    {
        if (y & 1)
            res = (res*x) % p;

        y = y>>1;
        x = (x*x) % p;
    }
    return res;
}

ll modFact(ll n, ll p)
{
    if (n >= p)
        return 0;

    ll result = 1;
    for (ll i = 1; i <= n; i++)
        result = (result * i) % p;

    return result;
}

ll binarySearch(ll arr[], ll l, ll r, ll x)
{
    if (r >= l) {
        ll mid = l + (r - l) / 2;

        if (arr[mid] == x)
            return mid;

        if (arr[mid] > x)
            return binarySearch(arr, l, mid - 1, x);

        return binarySearch(arr, mid + 1, r, x);
    }
    return -1;
}

ll decrease(int x,int y,int p ,int q)
{
    ll low = 1;
    ll high = LLONG_MAX;

    ll res = -1;

    while(low<=high)
    {
        ll mid = low + (high-low)/2;
cout<<"d"<<low<<" "<<high<<" "<<mid<<endl;
        if((long double)x/(y+mid) <= (long double)p/q)
        {
            res = mid;
            high = mid-1;
        }
        else
        {
            low = mid+1;
        }
    }

    return res;
}

ll increase(int x,int y,int p ,int q)
{
    ll low = 1;
    ll high = LLONG_MAX;

    ll res = -1;

    while(low<=high)
    {
        ll mid = low + (high-low)/2;
// cout<<"i"<<low<<" "<<high<<" "<<mid<<endl;
        if((long double)(x+mid)/(y+mid) >= (long double)p/q)
        {
            res = mid;
            high = mid-1;
        }
        else
        {
            low = mid+1;
        }

        cout<<res<<endl;
    }

    return res;
}

int main() {
    io;
    ll maxm=0,ans=0,sum=0,count=0,minm=1000000000000;

    ll tc,m,n,k,t,x,y,p,q;
    ll f=0;

    cin>>tc;
    while(tc--)
    {
        cin>>x>>y>>p>>q;

        if(p==0)
        {
            cout << (x == 0 ? 0 : -1 ) << endl;
            continue;
        }

        if(p == q)
        {
            cout << (x==y ? 0 : -1) << endl;
            continue;
        }

        t = max ( ( x+p-1 ) / p , ( (y-x) + (q-p-1) ) / (q-p) );

        cout << q * t - y << endl;
    }

    return 0;
}

// 127371699 575154303 513866599 558504245
// g++ -std=c++14 code.cpp


#include <iostream>
#include <cstdio>
#include <fstream>
#include <algorithm>
#include <cmath>
#include <deque>
#include <vector>
#include <queue>
#include <string>
#include <cstring>
#include <map>
#include <stack>
#include <set>

using namespace std;

int gcd(int a,int b)
{
    return (a%b)?gcd(b,a%b):b;
}

int main()
{
  //  freopen("a.txt","r",stdin);
    int T;
    int x,y,q,p,t1,t2,t;
    scanf("%d",&T);

    while(T--)
    {
        scanf("%d%d%d%d",&x,&y,&p,&q);

        if(p == q)
        {
            printf("%d\n",(x == y)?0:-1);
            continue;
        }

        if(!p)
        {
            printf("%d\n",(x)?-1:-0);
            continue;
        }

        t = gcd(q,p);

        p /= t;
        q /= t;

        int t1 = ceil(1.0*x/p);
        int t2 = ceil(1.0*(y-x)/(q-p));
        if(t1 > t2)t = t1;
        else t = t2;

        printf("%I64d\n",t*q - y);

    }
    return 0;
}


#include <iostream>
#include <cstdio>
#include <fstream>
#include <algorithm>
#include <cmath>
#include <deque>
#include <vector>
#include <queue>
#include <string>
#include <cstring>
#include <map>
#include <stack>
#include <set>

using namespace std;

int main()
{
    int T;
    int x,y,q,p,t1,t2;
    scanf("%d",&T);

    while(T--)
    {
        scanf("%d%d%d%d",&x,&y,&p,&q);

        if(p == q)
        {
            printf("-1\n");
            continue;
        }

        t1 = p;
        t2 = q;

        while(t1 - x > t2 - y || t1 - x < 0)
        {
            while(t2 < y)
            {
                t1 += p;
                t2 += q;
            }
            if(t1 - x > t2 - y || t1 - x < 0)
            {
                t1 += p;
                t2 += q;
            }
        }

        printf("%d\n",t2 - y);

    }
    return 0;
}


#include <iostream>
#include <cstdio>
#include <fstream>
#include <algorithm>
#include <cmath>
#include <deque>
#include <vector>
#include <queue>
#include <string>
#include <cstring>
#include <map>
#include <stack>
#include <set>

using namespace std;

int gcd(int a,int b)
{
    return (a%b)?gcd(b,a%b):b;
}

int main()
{
    freopen("a.txt","r",stdin);
    int T;
    int x,y,q,p,t1,t2,t;
    scanf("%d",&T);

    while(T--)
    {
        scanf("%d%d%d%d",&x,&y,&p,&q);

        if(p == q)
        {
            printf("%d\n",(x == y)?0:-1);
            continue;
        }

        if(!p)
        {
            printf("%d\n",(x)?-1:-0);
            continue;
        }

        t = gcd(q,p);

        p /= t;
        q /= t;

        int t1 = ceil(1.0*x/p);
        int t2 = ceil(1.0*(y-x)/(q-p));
        if(t1 > t2)t = t1;
        else t = t2;

        printf("%d\n",t*q - y);

    }
    return 0;
}


#include <iostream>
#include <cstdio>
#include <fstream>
#include <algorithm>
#include <cmath>
#include <deque>
#include <vector>
#include <queue>
#include <string>
#include <cstring>
#include <map>
#include <stack>
#include <set>

using namespace std;

int gcd(int a,int b)
{
    return (a%b)?gcd(b,a%b):b;
}

int main()
{
    //freopen("a.txt","r",stdin);
    int T;
    int x,y,q,p;
    long long t1,t2,t;
    scanf("%d",&T);

    while(T--)
    {
        scanf("%d%d%d%d",&x,&y,&p,&q);

        if(p == q)
        {
            printf("%d\n",(x == y)?0:-1);
            continue;
        }

        if(!p)
        {
            printf("%d\n",(x)?-1:-0);
            continue;
        }

        t = gcd(q,p);

        p /= t;
        q /= t;

        int t1 = ceil(1.0*x/p);
        int t2 = ceil(1.0*(y-x)/(q-p));
        if(t1 > t2)t = t1;
        else t = t2;

        printf("%I64d\n",t*q - y);

    }
    return 0;
}


#include <iostream>
#include <cstdio>
#include <fstream>
#include <algorithm>
#include <cmath>
#include <deque>
#include <vector>
#include <queue>
#include <string>
#include <cstring>
#include <map>
#include <stack>
#include <set>

using namespace std;

int gcd(int a,int b)
{
    return (a%b)?gcd(b,a%b):b;
}

int main()
{
    //freopen("a.txt","r",stdin);
    int T;
    int x,y,q,p,t1,t2,t;
    scanf("%d",&T);

    while(T--)
    {
        scanf("%d%d%d%d",&x,&y,&p,&q);

        if(p == q)
        {
            printf("%d\n",(x == y)?0:-1);
            continue;
        }

        if(!p)
        {
            printf("%d\n",(x)?-1:-0);
            continue;
        }

        t = gcd(q,p);

        p /= t;
        q /= t;

        int t1 = ceil(1.0*x/p);
        int t2 = ceil(1.0*(y-x)/(q-p));
        if(t1 > t2)t = t1;
        else t = t2;

        printf("%d\n",t*q - y);

    }
    return 0;
}


#include <bits/stdc++.h>
using namespace std;
int main() {
    int t;
    cin  >>  t;
    while(t--) {
        long long x , y , p , q;
        cin >> x >> y >> p >> q;
        if(p == q && x != y) {
            cout << -1 << endl;
            continue;
        }
        int g = __gcd(p , q);
        p /= g;
        q /= g;
        if(q * x >= p * y) {
            cout << 0 << endl;
            continue;
        }
        long long s = x / p;
        long long ss = x % p;
        if((s + 1) * q >= y + (p - ss)) {
            cout << p - ss << endl;
            continue;
        }
        long long tmp = y / q;
        tmp++;
        cout << tmp * q - x << endl;
    }

    return 0;
}


#include <bits/stdc++.h>
using namespace std;
int main() {
    int t;
    cin >> t;
    while(t--) {
        long long x , y , p , q;
        cin >> x >> y >> p >> q;
        long long ty = y;
        long long tx = x;
        if((p == q && x != y) || (x && !p)) {
            cout << -1 << endl;
            continue;
        }
        if(y % q) {
            ty = (y / q + 1) * q;
        }
        if(!p) {
            cout << 0 << endl;
            continue;
        }
        if(x % p) {
            tx = (x / p + 1) * p;
        }
        if(x * q >= p * y) {
            cout << tx / p * q - y << endl;
        }
        else {
            long long tx = x + ty - y;
            long long c1 = ty - tx;
            long long c2 = q - p;
            if(!(c1 % c2)) {
                cout << q * (c1 / c2) - y <<endl;
            }
            else cout << q *(c1 / c2 + 1) - y << endl;
        }
    }
    return 0;
}


#include <bits/stdc++.h>
using namespace std;
int main() {
    int t;
    cin >> t;
    while(t--) {
        long long x , y , p , q;
        cin >> x >> y >> p >> q;
        long long ty = y;
        long long tx = x;
        if((p == q && x != y) ||(x && !p)) {
            cout << -1 << endl;
            continue;
        }
        if(y % q) {
            ty = (y / q + 1) * q;
        }
        if(x % p) {
            tx = (x / p + 1) * p;
        }
        if(x * q >= p * y) {
            cout << tx / p * q - y << endl;
        }
        else {
            long long tx = x + ty - y;
            long long c1 = ty - tx;
            long long c2 = q - p;
            if(!(c1 % c2)) {
                cout << q * (c1 / c2) - y <<endl;
            }
            else cout << q *(c1 / c2 + 1) - y << endl;
        }
    }
    return 0;
}


//Author: codemaster24
//WA/TLE
#include<bits/stdc++.h>
#define ll          long long
#define pb          push_back
#define ppb         pop_back
#define	endl		'\n'
#define mii         map<ll int,ll int>
#define msi         map<string,ll int>
#define mis         map<ll int, string>
#define rep(i,a,b)    for(ll int i=a;i<b;i++)
#define tr(container,it) for(__typeof__(container.begin()) it = container.begin(); it != container.end(); it++)
#define pii         pair<ll int,ll int>
#define vi          vector<ll int>
#define mpi         map<pair<ll int,ll int>,ll int>
#define vii         vector<pair<ll,ll>>
#define vs          vector<string>
#define all(a)      (a).begin(),(a).end()
#define F           first
#define S           second
#define sz(x)       (ll)x.size()
#define hell        1000000007
#define lbnd        lower_bound
#define ubnd        upper_bound
#define bs          binary_search
#define mp          make_pair
#define what_is(x)  cerr << #x << " is " << x << endl;
using namespace std;
#define N  100005
#define time        cerr << "\nTime elapsed: " << 1000 * clock() / CLOCKS_PER_SEC << "ms\n";

int main()
{
	ios_base::sync_with_stdio(false);
	cin.tie(0);
	cout.tie(0);
	ll TESTS=1;
	cin>>TESTS;
	while(TESTS--){
		ll x,y,p,q;
		cin>>x>>y>>p>>q;
		if(p==q){
			if(x==y)
				cout<<0<<endl;
			else
				cout<<-1<<endl;
			continue;
		}
		if(p==0){
			if(ceil((y-x)/((q-p)*1.0))*q-y<0)
				cout<<-1<<endl;
			else
			cout<<ceil((y-x)/((q-p)*1.0))*q-y<<endl;
			continue;
		}
		ll t=max(ceil(x/(p*1.0)),ceil((y-x)/((q-p)*1.0)));
		if(t*q-y<0)
			cout<<-1<<endl;
		else
		cout<<t*q-y<<endl;

	}
	return 0;
}


//Author: codemaster24
//WA/TLE
#include<bits/stdc++.h>
#define ll          long long
#define pb          push_back
#define ppb         pop_back
#define	endl		'\n'
#define mii         map<ll int,ll int>
#define msi         map<string,ll int>
#define mis         map<ll int, string>
#define rep(i,a,b)    for(ll int i=a;i<b;i++)
#define tr(container,it) for(__typeof__(container.begin()) it = container.begin(); it != container.end(); it++)
#define pii         pair<ll int,ll int>
#define vi          vector<ll int>
#define mpi         map<pair<ll int,ll int>,ll int>
#define vii         vector<pair<ll,ll>>
#define vs          vector<string>
#define all(a)      (a).begin(),(a).end()
#define F           first
#define S           second
#define sz(x)       (ll)x.size()
#define hell        1000000007
#define lbnd        lower_bound
#define ubnd        upper_bound
#define bs          binary_search
#define mp          make_pair
#define what_is(x)  cerr << #x << " is " << x << endl;
using namespace std;
#define N  100005
#define time        cerr << "\nTime elapsed: " << 1000 * clock() / CLOCKS_PER_SEC << "ms\n";

int main()
{
	ios_base::sync_with_stdio(false);
	cin.tie(0);
	cout.tie(0);
	ll TESTS=1;
	cin>>TESTS;
	while(TESTS--){
		ll x,y,p,q;
		cin>>x>>y>>p>>q;
		if(p==q){
			if(x==y)
				cout<<0<<endl;
			else
				cout<<-1<<endl;
			continue;
		}
		ll t=ceil(y/(q*1.0));
		while(1){
			if((t*q-y)>=(t*p-x)&&(t*p-x)>=0){
				cout<<t*q-y<<endl;
				break;
			}
			t++;
			if(t*p-x<0){
				t=ceil(x/(p*1.0));
			}
			t=max(ceil(x/(p*1.0)),ceil((y-x)/((q-p)*1.0)));
		}

	}
	return 0;
}


//Author: codemaster24
//WA/TLE
#include<bits/stdc++.h>
#define ll          long long
#define pb          push_back
#define ppb         pop_back
#define	endl		'\n'
#define mii         map<ll int,ll int>
#define msi         map<string,ll int>
#define mis         map<ll int, string>
#define rep(i,a,b)    for(ll int i=a;i<b;i++)
#define tr(container,it) for(__typeof__(container.begin()) it = container.begin(); it != container.end(); it++)
#define pii         pair<ll int,ll int>
#define vi          vector<ll int>
#define mpi         map<pair<ll int,ll int>,ll int>
#define vii         vector<pair<ll,ll>>
#define vs          vector<string>
#define all(a)      (a).begin(),(a).end()
#define F           first
#define S           second
#define sz(x)       (ll)x.size()
#define hell        1000000007
#define lbnd        lower_bound
#define ubnd        upper_bound
#define bs          binary_search
#define mp          make_pair
#define what_is(x)  cerr << #x << " is " << x << endl;
using namespace std;
#define N  100005
#define time        cerr << "\nTime elapsed: " << 1000 * clock() / CLOCKS_PER_SEC << "ms\n";

int main()
{
	ios_base::sync_with_stdio(false);
	cin.tie(0);
	cout.tie(0);
	ll TESTS=1;
	cin>>TESTS;
	while(TESTS--){
		ll x,y,p,q;
		cin>>x>>y>>p>>q;
		if(p==q){
			if(x==y)
				cout<<0<<endl;
			else
				cout<<-1<<endl;
			continue;
		}
		ll t=ceil(y/(q*1.0));
		while(1){
			if((t*q-y)>=(t*p-x)&&(t*p-x)>=0){
				cout<<t*q-y<<endl;
				break;
			}
			t++;
		}
	}
	return 0;
}


//Author: codemaster24
//WA/TLE
#include<bits/stdc++.h>
#define ll          long long
#define pb          push_back
#define ppb         pop_back
#define	endl		'\n'
#define mii         map<ll int,ll int>
#define msi         map<string,ll int>
#define mis         map<ll int, string>
#define rep(i,a,b)    for(ll int i=a;i<b;i++)
#define tr(container,it) for(__typeof__(container.begin()) it = container.begin(); it != container.end(); it++)
#define pii         pair<ll int,ll int>
#define vi          vector<ll int>
#define mpi         map<pair<ll int,ll int>,ll int>
#define vii         vector<pair<ll,ll>>
#define vs          vector<string>
#define all(a)      (a).begin(),(a).end()
#define F           first
#define S           second
#define sz(x)       (ll)x.size()
#define hell        1000000007
#define lbnd        lower_bound
#define ubnd        upper_bound
#define bs          binary_search
#define mp          make_pair
#define what_is(x)  cerr << #x << " is " << x << endl;
using namespace std;
#define N  100005
#define time        cerr << "\nTime elapsed: " << 1000 * clock() / CLOCKS_PER_SEC << "ms\n";

int main()
{
	ios_base::sync_with_stdio(false);
	cin.tie(0);
	cout.tie(0);
	ll TESTS=1;
	cin>>TESTS;
	while(TESTS--){
		ll x,y,p,q;
		cin>>x>>y>>p>>q;
		if(p==q){
			if(x==y)
				cout<<0<<endl;
			else
				cout<<-1<<endl;
			continue;
		}
		if(p==0){
			cout<<ceil((y-x)/((q-p)*1.0))*q-y<<endl;
			continue;
		}
		ll t=max(ceil(x/(p*1.0)),ceil((y-x)/((q-p)*1.0)));
		cout<<t*q-y<<endl;

	}
	return 0;
}


//Author: codemaster24
//WA/TLE
#include<bits/stdc++.h>
#define ll          long long
#define pb          push_back
#define ppb         pop_back
#define	endl		'\n'
#define mii         map<ll int,ll int>
#define msi         map<string,ll int>
#define mis         map<ll int, string>
#define rep(i,a,b)    for(ll int i=a;i<b;i++)
#define tr(container,it) for(__typeof__(container.begin()) it = container.begin(); it != container.end(); it++)
#define pii         pair<ll int,ll int>
#define vi          vector<ll int>
#define mpi         map<pair<ll int,ll int>,ll int>
#define vii         vector<pair<ll,ll>>
#define vs          vector<string>
#define all(a)      (a).begin(),(a).end()
#define F           first
#define S           second
#define sz(x)       (ll)x.size()
#define hell        1000000007
#define lbnd        lower_bound
#define ubnd        upper_bound
#define bs          binary_search
#define mp          make_pair
#define what_is(x)  cerr << #x << " is " << x << endl;
using namespace std;
#define N  100005
#define time        cerr << "\nTime elapsed: " << 1000 * clock() / CLOCKS_PER_SEC << "ms\n";

int main()
{
	ios_base::sync_with_stdio(false);
	cin.tie(0);
	cout.tie(0);
	ll TESTS=1;
	cin>>TESTS;
	while(TESTS--){
		ll x,y,p,q;
		cin>>x>>y>>p>>q;
		if(p==q){
			if(x==y)
				cout<<0<<endl;
			else
				cout<<-1<<endl;
			continue;
		}
		ll t=ceil(y/(q*1.0));
		while(1){
			if((t*q-y)>=(t*p-x)&&(t*p-x)>=0){
				cout<<t*q-y<<endl;
				break;
			}
			t++;
			if(t*p-x<0){
				t=ceil(x/(p*1.0));
			}
		}

	}
	return 0;
}


//Author: codemaster24
//WA/TLE
#include<bits/stdc++.h>
#define ll          long long
#define pb          push_back
#define ppb         pop_back
#define	endl		'\n'
#define mii         map<ll int,ll int>
#define msi         map<string,ll int>
#define mis         map<ll int, string>
#define rep(i,a,b)    for(ll int i=a;i<b;i++)
#define tr(container,it) for(__typeof__(container.begin()) it = container.begin(); it != container.end(); it++)
#define pii         pair<ll int,ll int>
#define vi          vector<ll int>
#define mpi         map<pair<ll int,ll int>,ll int>
#define vii         vector<pair<ll,ll>>
#define vs          vector<string>
#define all(a)      (a).begin(),(a).end()
#define F           first
#define S           second
#define sz(x)       (ll)x.size()
#define hell        1000000007
#define lbnd        lower_bound
#define ubnd        upper_bound
#define bs          binary_search
#define mp          make_pair
#define what_is(x)  cerr << #x << " is " << x << endl;
using namespace std;
#define N  100005
#define time        cerr << "\nTime elapsed: " << 1000 * clock() / CLOCKS_PER_SEC << "ms\n";

int main()
{
	ios_base::sync_with_stdio(false);
	cin.tie(0);
	cout.tie(0);
	ll TESTS=1;
	cin>>TESTS;
	while(TESTS--){
		ll x,y,p,q;
		cin>>x>>y>>p>>q;
		if(p==q){
			if(x==y)
				cout<<0<<endl;
			else
				cout<<-1<<endl;
			continue;
		}
		if(p==0){
			cout<<ceil((y-x)/((q-p)*1.0))*q-y<<endl;
		}
		ll t=max(ceil(x/(p*1.0)),ceil((y-x)/((q-p)*1.0)));
		cout<<t*q-y<<endl;

	}
	return 0;
}


//Author: codemaster24
//WA/TLE
#include<bits/stdc++.h>
#define ll          long long
#define pb          push_back
#define ppb         pop_back
#define	endl		'\n'
#define mii         map<ll int,ll int>
#define msi         map<string,ll int>
#define mis         map<ll int, string>
#define rep(i,a,b)    for(ll int i=a;i<b;i++)
#define tr(container,it) for(__typeof__(container.begin()) it = container.begin(); it != container.end(); it++)
#define pii         pair<ll int,ll int>
#define vi          vector<ll int>
#define mpi         map<pair<ll int,ll int>,ll int>
#define vii         vector<pair<ll,ll>>
#define vs          vector<string>
#define all(a)      (a).begin(),(a).end()
#define F           first
#define S           second
#define sz(x)       (ll)x.size()
#define hell        1000000007
#define lbnd        lower_bound
#define ubnd        upper_bound
#define bs          binary_search
#define mp          make_pair
#define what_is(x)  cerr << #x << " is " << x << endl;
using namespace std;
#define N  100005
#define time        cerr << "\nTime elapsed: " << 1000 * clock() / CLOCKS_PER_SEC << "ms\n";

int main()
{
	ios_base::sync_with_stdio(false);
	cin.tie(0);
	cout.tie(0);
	ll TESTS=1;
	cin>>TESTS;
	while(TESTS--){
		ll x,y,p,q;
		cin>>x>>y>>p>>q;
		if(p==q){
			if(x==y)
				cout<<0<<endl;
			else
				cout<<-1<<endl;
			continue;
		}
		ll t=max(ceil(x/(p*1.0)),ceil((y-x)/((q-p)*1.0)));
		cout<<t*q-y<<endl;

	}
	return 0;
}


#include<bits/stdc++.h>
using namespace std;
int main()
{
	int t;
	cin>>t;
	while(t--)
	{
		long long int x,y,p,q;
		cin>>x>>y>>p>>q;
		long long int a,b,l=0,h=2e9,ans=-1,mid;
		cout<<h<<endl;

		while(l<=h)
		{
			mid=(l+h)/2;
			a=mid*p;
			b=mid*q;
			if(a>=x && b>=y && b-a>=y-x)
			{
				ans=b-y;
				h=mid-1;
			}
			else
			{
				l=mid+1;
			}
		}
		cout<<ans<<endl;

	}
}


#include<bits/stdc++.h>
using namespace std;
int main()
{
	int t;
	cin>>t;
	while(t--)
	{
		long long int x,y,p,q;
		cin>>x>>y>>p>>q;
		long long int a,b,l=0,h=2e9,ans=-1,mid;
	//	cout<<h<<endl;

		while(l<=h)
		{
			mid=(l+h)/2;
			a=mid*p;
			b=mid*q;
			if(a>=x && b>=y && b-a>=y-x)
			{
				ans=b-y;
				h=mid-1;
			}
			else
			{
				l=mid+1;
			}
		}
		cout<<ans<<endl;

	}
}


#include<bits/stdc++.h>
using namespace std;
#define int     long long
#define ld      long double
#define pb      push_back
#define pa      pair<int,int>
#define v1d     vector<int>
#define v2d     vector<vector<int> >
#define M       1000000007
#define mp      make_pair
#define MN      LLONG_MIN
#define MX      LLONG_MAX
#define ff      first
#define ss      second
#define ump     map<int,int>
#define endl    "\n"
#define v1s     vector<string>
#define S(v)    sort(v.begin(),v.end())
#define RS(v)   sort(v.rbegin(),v.rend())
#define mxpq    priority_queue <int>
#define mnpq    priority_queue<int,vector<int>,greater<int>>
#define T       int tc;cin>>tc;while(tc--)
void Weapons19(){
    cin.sync_with_stdio(false);
    cin.tie(0);cout.tie(0);
    //freopen("input.txt", "r", stdin);
    //freopen("output.txt", "w", stdout);
}

int32_t main(){
    Weapons19;
    T{
        int a,b,p,q;cin>>a>>b>>p>>q;
        //int x=__gcd(a,b);a/=x;b/=x;
        int st=1,en=1000000000;
        while(st!=en){
            int mid=(st+en)/2;
            if(p*mid>=a&&q*mid>=b&&p*mid-a<=q*mid-b)en=mid;
            else st=mid+1;
        }
        if(st==1000000000)cout<<"-1\n";
        else
        cout<<q*st-b<<endl;
    }

}


#include<bits/stdc++.h>
using namespace std;
#define int     long long
#define ld      long double
#define pb      push_back
#define pa      pair<int,int>
#define v1d     vector<int>
#define v2d     vector<vector<int> >
#define M       1000000007
#define mp      make_pair
#define MN      LLONG_MIN
#define MX      LLONG_MAX
#define ff      first
#define ss      second
#define ump     map<int,int>
#define endl    "\n"
#define v1s     vector<string>
#define S(v)    sort(v.begin(),v.end())
#define RS(v)   sort(v.rbegin(),v.rend())
#define mxpq    priority_queue <int>
#define mnpq    priority_queue<int,vector<int>,greater<int>>
#define T       int tc;cin>>tc;while(tc--)
void Weapons19(){
    cin.sync_with_stdio(false);
    cin.tie(0);cout.tie(0);
    //freopen("input.txt", "r", stdin);
    //freopen("output.txt", "w", stdout);
}

int32_t main(){
    Weapons19;
    T{
        int a,b,p,q;cin>>a>>b>>p>>q;
        //int x=__gcd(a,b);a/=x;b/=x;
        int st=1,en=1000000007;
        while(st!=en){
            int mid=(st+en)/2;
            if(p*mid>=a&&q*mid>=b&&p*mid-a<=q*mid-b)en=mid;
            else st=mid+1;
        }
        if(st==1000000007)cout<<"-1\n";
        else
        cout<<q*st-b<<endl;
    }

}


#include <iostream>
#include <fstream>
#include <string>
#include <algorithm>
#include <vector>
#include <queue>
#include <stack>
#include <set>
#include <map>
#include <cstdlib>
#include <cmath>
#include <iomanip>
#include <iterator>
#include <ctime>
#include <random>
using namespace std;
void solve(),solve1();
int n,k;
int main(int argc, const char * argv[]) {
    srand((unsigned int)time(0));
    ios::sync_with_stdio(0);
    //solve1();
    solve();
    return 0;
}
#define int long long
#define lli long long
#define mp make_pair
#define sqr(x) ((x)*(x))
#define double long double
int mod = 1e9;
int inf = LONG_LONG_MAX;
int gcd (int a, int b) {
    while (b) {
        a %= b;
        swap (a, b);
    }
    return a;
}

int f(int x, int y, int p, int q)
{
    if (p == 0)
    {
        if (x==0)
            return  0;
        else
            return -1;
    }
    if (p==q)
    {
        if (x==y)
            return 0;
        else
            return  -1;
    }

    int l = 0, r = 2e9;
    while (l<r)
    {
        int mid = l+r>>1;
        int qq = q*mid;
        int pp = p*mid;
        int res = qq-y;
        if (res>=0 && x<= pp && x+res>=pp)
            r = mid;
        else
            l = mid+1;
    }
    return l*q-y;

}

void solve()
{
    //  ifstream cin("input.txt");
    // ofstream cout("output.txt");
    int t;
    cin >> t;
    for (int T = 0; T<t; T++)
    {
        int x,y,p,q;
        cin >> x >> y >> p >> q;
        cout << f(x,y,p,q) << endl;
    }

}


#include <iostream>
#include <fstream>
#include <string>
#include <algorithm>
#include <vector>
#include <queue>
#include <stack>
#include <set>
#include <map>
#include <cstdlib>
#include <cmath>
#include <iomanip>
#include <iterator>
#include <ctime>
#include <random>
using namespace std;
void solve(),solve1();
int n,k;
int main(int argc, const char * argv[]) {
    srand((unsigned int)time(0));
    ios::sync_with_stdio(0);
    //solve1();
    solve();
    return 0;
}
#define int long long
#define lli long long
#define mp make_pair
#define sqr(x) ((x)*(x))
#define double long double
int mod = 1e9;
int inf = LONG_LONG_MAX;
int gcd (int a, int b) {
    while (b) {
        a %= b;
        swap (a, b);
    }
    return a;
}

int f(int x, int y, int p, int q)
{
    if (p == 0)
    {
        if (x==0)
            return  0;
        else
            return -1;
    }
    if (p==q)
    {
        if (x==y)
            return 0;
        else
            return  -1;
    }

    int l = 0, r = 2e9;
    while (l<r)
    {
        int mid = l+r>>1;
        int qq = q*mid;
        int pp = p*mid;
        int res = qq-y;
        if (res>=0 && x<= pp && x+res>=pp)
            r = mid;
        else
            l = mid+1;
    }
    return l*mid-y;

}

void solve()
{
    //  ifstream cin("input.txt");
    // ofstream cout("output.txt");
    int t;
    cin >> t;
    for (int T = 0; T<t; T++)
    {
        int x,y,p,q;
        cin >> x >> y >> p >> q;
        cout << f(x,y,p,q) << endl;
    }

}


#include<bits/stdc++.h>
// #include<ext/pb_ds/assoc_container.hpp>
// #include<ext/pb_ds/tree_policy.hpp>
using namespace std;
    // using namespace __gnu_pbds;

typedef long long int lli;
typedef unsigned long long int ull;
typedef long double ld;
typedef pair<lli,lli> pll;
typedef std::vector<lli> vll;
typedef std::vector<pll> vpll;
typedef std::vector<bool> vbb;
typedef map<lli,lli>mll;
typedef set<lli> sll;
typedef multiset<lli> msll;
lli MOD=1e9 +7;
lli INF=1e18;

#define forr(i,p,n) for(i=p; i<n; i++)
#define forn(i,p,n) for(i=p; i<=n; i++)
#define forl(i,n,p) for(i=n; i>=p; i--)
#define be(a) a.begin(),a.end()
#define mp make_pair
#define eb emplace_back
#define pb push_back
#define F first
#define S second
#define all(a)     (a).begin(),(a).end()
#define PI 3.14159265358979323846
#define lb lower_bound
#define ub upper_bound
#define endl "\n"
#define bpcl(x) __builtin_popcountll(x)
#define deb(vari) cerr<<#vari<<" = "<<vari<<endl;
            // #define ordered_set tree<lli, null_type,less<lli>, rb_tree_tag,tree_order_statistics_node_update>
lli power(lli base, lli exp) {lli res=1;while(exp>0) {if(exp%2==1) res=(res*base);base=(base*base);exp/=2;}return res;}
lli bitc(lli n,lli x){return ((n>>x)&1);}
lli __gcd(lli a,lli b){return b==0?a:__gcd(b,a%b);}
lli fsub(lli a,lli b,lli p=MOD){return ((a%p)-(b%p)+p)%p;}
lli fmult(lli a,lli b, lli p=MOD) {return ((a%p)*(b%p))%p;}
lli fadd(lli a, lli b, lli p=MOD) {return (a%p + b%p)%p;}
                                    // lli add(lli a,lli b,lli p=MOD){//if(a+b>=p){//return (a+b)-p;//  }//return a+b;// }
lli fpow(lli n, lli k, lli p = MOD) {lli r = 1; while(k > 0) {if (k & 1) r = r * n%p; n = n * n%p; k = k >> 1;} return r;}
lli inv(lli a, lli p = MOD) {return fpow(a, p - 2, p);}
lli fdiv(lli a,lli b,lli p=MOD){lli yinv=inv(b);lli ans=(a*yinv)%p;return ans;}

template<typename T>istream &operator>>(istream &in, vector<T> &a) {for (auto &item : a) {in >> item;}return in;}template<typename T,typename U>ostream &operator<<(ostream &out, pair<T,U> &a) {cout<<a.F<<" "<<a.S;return out;}
template<typename T,typename U>
istream &operator>>(istream &out, pair<T,U> &a) {cin>>a.F>>a.S;return out;}
template<typename T,typename U>
ostream &operator<<(ostream &out, map<T,U> &a) {for (auto &item : a) {out << item << endl;}return out;}
template<typename T>
ostream &operator<<(ostream &out, vector<T> &a) {for (auto &item : a) {out << item << " ";}return out;}
template<typename T>
ostream &operator<<(ostream &out, vector<vector<T>> &a) {for (auto &item : a) {out << item << "\n";}return out;}
template<int D, typename T>
struct Vec : public vector<Vec<D - 1, T>> {
  static_assert(D >= 1, "Vector dimension must be greater than zero!");
                      template<typename... Args>
  Vec(int n = 0, Args... args) : vector<Vec<D - 1, T>>(n, Vec<D - 1, T>(args...)) {
  }
};
template<typename T>
struct Vec<1, T> : public vector<T> {
  Vec(int n = 0, T val = T()) : vector<T>(n, val) {
  }
};
                // Vec<2,lli>mat(n,n,1);
#define print(a) for(auto p:a) cout<<p<<endl; cout<<endl;
#define read(a) for(auto &p:a)cin>>p>>endl;
std::vector<bool> is_prime;
std::vector<lli> primes;
void sieve(lli n){is_prime.resize(n+2,true);primes.clear();lli p;for(p=2;p*p<=n;p++){if(is_prime[p]){lli i;for(i=p*p;i<=n;i+=p){is_prime[i]=false;}}}is_prime[0]=is_prime[1]=false;lli i;for(i=2;i<=n;i++){if(is_prime[i]){primes.eb(i);}}}
mll prime_factors(lli n){  mll s;lli i;lli tc=0;while(n%2==0){tc++;n/=2;}if(tc>0){s[2]=tc;}for(i=3;i<=sqrt(n);i+=2){tc=0;while(n%i==0){tc++;n/=i;}if(tc>0){s[i]=tc;}}if(n>2){s[n]+=1;}return s;}
std::vector<lli> fact_vec;
void fact_fun(lli n){fact_vec.resize(n+10);lli i;fact_vec[0]=1;for(i=1;i<=n+2;i++){fact_vec[i]=(fact_vec[i-1]*i)%MOD;}}
vll p2;
void power_2(lli n,lli m=MOD){ lli i;MOD=m;p2.eb(1);forr(i,0,n){p2.eb(fmult(p2.back(),2));}}
lli ncr(lli n,lli r){if(r>n)return 0;return fdiv(fact_vec[n],fmult(fact_vec[r],fact_vec[n-r]));}
vll spf;void sieve2(lli MAXN) { MAXN+=10;spf.resize(MAXN,0);spf[1] = 1; for (lli i=2; i<MAXN; i++) spf[i] = i;     for (lli i=4; i<MAXN; i+=2) spf[i] = 2; for (lli i=3; i*i<MAXN; i++) {         if (spf[i] == i) { for (lli j=i*i; j<MAXN; j+=i) {if (spf[j]==j) spf[j] = i; }} } }

vll getFactorization(lli x)
{
    vll ret;
    while (x != 1){
        ret.eb(spf[x]);
        x = x / spf[x];
    } return ret;
}

// class Sol{
// public:
//     Sol(){
//     }
// };

int main(){
    lli i,j;

        #ifndef ONLINE_JUDGE
    freopen("input.txt", "r", stdin);
    freopen("output.txt",  "w", stdout);
        #endif

    ios::sync_with_stdio(false);
    cin.tie(0);

    lli ti;

    ti=1;
        cin>>ti;
    while(ti--){
        lli x,y,p,q;
        cin>>x>>y>>p>>q;
        lli l =-1;
        lli r=1e9;
        auto check=[&](lli t){
            return (p*t>=x) && ((q*t-p*t)>=(y-x));
        };
        lli ans=INF;
        while(l<=r){
            lli  mid=(l+r)/2;
            if(check(mid)){
                ans=mid;
                r=mid-1;
            }else{
                l=mid+1;
            }
        }
        if(ans==INF){
            cout<<-1<<endl;
            return 0;
        }
        ans= ans*q-y;
        cout<<ans<<endl;

    }

}


#include<bits/stdc++.h>
// #include<ext/pb_ds/assoc_container.hpp>
// #include<ext/pb_ds/tree_policy.hpp>
using namespace std;
    // using namespace __gnu_pbds;

typedef long long int lli;
typedef unsigned long long int ull;
typedef long double ld;
typedef pair<lli,lli> pll;
typedef std::vector<lli> vll;
typedef std::vector<pll> vpll;
typedef std::vector<bool> vbb;
typedef map<lli,lli>mll;
typedef set<lli> sll;
typedef multiset<lli> msll;
lli MOD=1e9 +7;
lli INF=1e18;

#define forr(i,p,n) for(i=p; i<n; i++)
#define forn(i,p,n) for(i=p; i<=n; i++)
#define forl(i,n,p) for(i=n; i>=p; i--)
#define be(a) a.begin(),a.end()
#define mp make_pair
#define eb emplace_back
#define pb push_back
#define F first
#define S second
#define all(a)     (a).begin(),(a).end()
#define PI 3.14159265358979323846
#define lb lower_bound
#define ub upper_bound
#define endl "\n"
#define bpcl(x) __builtin_popcountll(x)
#define deb(vari) cerr<<#vari<<" = "<<vari<<endl;
            // #define ordered_set tree<lli, null_type,less<lli>, rb_tree_tag,tree_order_statistics_node_update>
lli power(lli base, lli exp) {lli res=1;while(exp>0) {if(exp%2==1) res=(res*base);base=(base*base);exp/=2;}return res;}
lli bitc(lli n,lli x){return ((n>>x)&1);}
lli __gcd(lli a,lli b){return b==0?a:__gcd(b,a%b);}
lli fsub(lli a,lli b,lli p=MOD){return ((a%p)-(b%p)+p)%p;}
lli fmult(lli a,lli b, lli p=MOD) {return ((a%p)*(b%p))%p;}
lli fadd(lli a, lli b, lli p=MOD) {return (a%p + b%p)%p;}
                                    // lli add(lli a,lli b,lli p=MOD){//if(a+b>=p){//return (a+b)-p;//  }//return a+b;// }
lli fpow(lli n, lli k, lli p = MOD) {lli r = 1; while(k > 0) {if (k & 1) r = r * n%p; n = n * n%p; k = k >> 1;} return r;}
lli inv(lli a, lli p = MOD) {return fpow(a, p - 2, p);}
lli fdiv(lli a,lli b,lli p=MOD){lli yinv=inv(b);lli ans=(a*yinv)%p;return ans;}

template<typename T>istream &operator>>(istream &in, vector<T> &a) {for (auto &item : a) {in >> item;}return in;}template<typename T,typename U>ostream &operator<<(ostream &out, pair<T,U> &a) {cout<<a.F<<" "<<a.S;return out;}
template<typename T,typename U>
istream &operator>>(istream &out, pair<T,U> &a) {cin>>a.F>>a.S;return out;}
template<typename T,typename U>
ostream &operator<<(ostream &out, map<T,U> &a) {for (auto &item : a) {out << item << endl;}return out;}
template<typename T>
ostream &operator<<(ostream &out, vector<T> &a) {for (auto &item : a) {out << item << " ";}return out;}
template<typename T>
ostream &operator<<(ostream &out, vector<vector<T>> &a) {for (auto &item : a) {out << item << "\n";}return out;}
template<int D, typename T>
struct Vec : public vector<Vec<D - 1, T>> {
  static_assert(D >= 1, "Vector dimension must be greater than zero!");
                      template<typename... Args>
  Vec(int n = 0, Args... args) : vector<Vec<D - 1, T>>(n, Vec<D - 1, T>(args...)) {
  }
};
template<typename T>
struct Vec<1, T> : public vector<T> {
  Vec(int n = 0, T val = T()) : vector<T>(n, val) {
  }
};
                // Vec<2,lli>mat(n,n,1);
#define print(a) for(auto p:a) cout<<p<<endl; cout<<endl;
#define read(a) for(auto &p:a)cin>>p>>endl;
std::vector<bool> is_prime;
std::vector<lli> primes;
void sieve(lli n){is_prime.resize(n+2,true);primes.clear();lli p;for(p=2;p*p<=n;p++){if(is_prime[p]){lli i;for(i=p*p;i<=n;i+=p){is_prime[i]=false;}}}is_prime[0]=is_prime[1]=false;lli i;for(i=2;i<=n;i++){if(is_prime[i]){primes.eb(i);}}}
mll prime_factors(lli n){  mll s;lli i;lli tc=0;while(n%2==0){tc++;n/=2;}if(tc>0){s[2]=tc;}for(i=3;i<=sqrt(n);i+=2){tc=0;while(n%i==0){tc++;n/=i;}if(tc>0){s[i]=tc;}}if(n>2){s[n]+=1;}return s;}
std::vector<lli> fact_vec;
void fact_fun(lli n){fact_vec.resize(n+10);lli i;fact_vec[0]=1;for(i=1;i<=n+2;i++){fact_vec[i]=(fact_vec[i-1]*i)%MOD;}}
vll p2;
void power_2(lli n,lli m=MOD){ lli i;MOD=m;p2.eb(1);forr(i,0,n){p2.eb(fmult(p2.back(),2));}}
lli ncr(lli n,lli r){if(r>n)return 0;return fdiv(fact_vec[n],fmult(fact_vec[r],fact_vec[n-r]));}
vll spf;void sieve2(lli MAXN) { MAXN+=10;spf.resize(MAXN,0);spf[1] = 1; for (lli i=2; i<MAXN; i++) spf[i] = i;     for (lli i=4; i<MAXN; i+=2) spf[i] = 2; for (lli i=3; i*i<MAXN; i++) {         if (spf[i] == i) { for (lli j=i*i; j<MAXN; j+=i) {if (spf[j]==j) spf[j] = i; }} } }

vll getFactorization(lli x)
{
    vll ret;
    while (x != 1){
        ret.eb(spf[x]);
        x = x / spf[x];
    } return ret;
}

// class Sol{
// public:
//     Sol(){
//     }
// };

int main(){
    lli i,j;

        #ifndef ONLINE_JUDGE
    freopen("input.txt", "r", stdin);
    freopen("output.txt",  "w", stdout);
        #endif

    ios::sync_with_stdio(false);
    cin.tie(0);

    lli ti;

    ti=1;
        cin>>ti;
    while(ti--){
        lli x,y,p,q;
        cin>>x>>y>>p>>q;
        lli l =-1;
        lli r=1e9;
        auto check=[&](lli t){
            return (p*t>=x) && ((q*t-p*t)>=(y-x));
        };
        if(!check(1e9)){
            cout<<-1<<endl;
            continue;
        }
        lli ans=INF;
        while(r-l>1){
            lli  mid=(l+r)/2;
            if(check(mid)){

                r=mid;
            }else{
                l=mid;
            }
        }
        ans= r*q-y;
        cout<<ans<<endl;

    }

}


#include <bits/stdc++.h>

using namespace std;

typedef long long ll;

ll gcd(ll a , ll b){
   if( b == 0 ) return a;
   return gcd(b ,a % b);
}

ll T, x, y, a, b;

bool f(ll c) {
    ll p = c * a - x;
    ll q = c * b - y;
    return p >= 0 && q >= 0 && p <= q;
}

ll ans(ll c){
    return c * b - y;
}

int main() {

    cin >> T;
    while(T--) {
        cin >> x >> y >> a >> b;

        a /= gcd(a, b);
        b /= gcd(a, b);

        if(x / gcd(x, y) == a && y/gcd(x,y) == b) {
            cout << 0 << endl;
            continue;
        }

        ll low = 0, high = 1e9;
        while(high - low > 1) {
            int mid = (low + high) / 2;
            if(f(mid))
                high = mid;
            else
                low = mid;
        }

        if(f(low))
            cout << ans(low);
        else if(f(high))
            cout << ans(high);
        else
            cout << -1;

        cout << endl;

    }
}


#include <bits/stdc++.h>

using namespace std;

int gcd(int a , int b){
   if( b == 0 ) return a;
   return gcd(b ,a % b);
}

int T, x, y, a, b;

bool f(int c) {
    int p = c * a - x;
    int q = c * b - y;
    return p >= 0 && q >= 0 && p <= q;
}

int ans(int c){
    return c * b - y;
}

int main() {

    cin >> T;
    while(T--) {
        cin >> x >> y >> a >> b;

        a /= gcd(a, b);
        b /= gcd(a, b);

        if(x / gcd(x, y) == a && y/gcd(x,y) == b) {
            cout << 0 << endl;
            continue;
        }

        int low = 0, high = 1e7;
        while(high - low > 1) {
            int mid = (low + high) / 2;
            if(f(mid))
                high = mid;
            else
                low = mid;
        }

        if(f(low))
            cout << ans(low);
        else if(f(high))
            cout << ans(high);
        else
            cout << -1;

        cout << endl;

    }
}


#include<iostream>
#include<algorithm>
#include<cstdio>
#include<string>
#include<cstring>
#include<cassert>
#include<cmath>
#include<sstream>
#include<fstream>
#include<map>
#include<set>
#include<vector>
#include<queue>
#include<stack>
#include<bitset>
using namespace std;
typedef long long ll;
ll inline gcd(ll a,ll b){
    int c=1;
    while(a-b){
        if(a&1){
            if(b&1){
                if(a>b)a=(a-b)>>1;else b=(b-a)>>1;
            }
            else b>>=1;
        }
        else{
            if(b&1)a>>=1;else c<<=1,a>>=1,b>>=1;
        }
    }
    return c*a;
}
ll inline ma(ll a,ll b)
{
	return a>b ? a:b;
}
int main()
{
	ios_base::sync_with_stdio(0);
	cin.tie(0);
	int n;
	cin>>n;
	for(int i=0;i<n;i++)
	{
		ll a,b,c,d;
		cin>>a>>b>>c>>d;
		if(a<b&&c==d||a!=0&&c==0)
		{
			cout<<-1<<endl;
			continue;
		}
		b-=a,d-=c;
		if(a==0&&c==0||!b&&!d)
		{
			cout<<0<<endl;
			continue;
		}
		ll x=gcd(c,d);
		c/=x,d/=x;
		ll re1=a/c+((a%c)!=0),re2=b/d+((b%d)!=0),re=ma(re1,re2);
		cout<<(d+c)*re-a-b<<endl;
	}
	return 0;
}


#include<iostream>
#include<algorithm>
#include<cstdio>
#include<string>
#include<cstring>
#include<cassert>
#include<cmath>
#include<sstream>
#include<fstream>
#include<map>
#include<set>
#include<vector>
#include<queue>
#include<stack>
#include<bitset>
using namespace std;
typedef long long ll;
ll inline gcd(ll a,ll b)
{
	return a? gcd(b%a,a):b;
}
ll inline ma(ll a,ll b)
{
	return a>b? a:b;
}
int main()
{
	ios_base::sync_with_stdio(0);
	cin.tie(0);
	int n;
	cin>>n;
	for(int i=0;i<n;i++)
	{
		ll a,b,c,d;
		cin>>a>>b>>c>>d;
		if(a<b&&c==d||a!=0&&c==0)
		{
			cout<<-1<<endl;
			continue;
		}
		b-=a,d-=c;
		if(a==0&&c==0||!b&&!d)
		{
			cout<<0<<endl;
			continue;
		}
		ll x=gcd(c,d);
		c/=x,d/=x;
		ll re1=a/c+((a%c)!=0),re2=b/d+((b%d)!=0),re=ma(re1,re2);
		cout<<(d+c)*re-a-b<<endl;
	}
	return 0;
}


#include <bits/stdc++.h>

using namespace std;

#define sz(v)           ((int)((v).size()))
#define clr(v, d)       memset(v, d, sizeof(v))
#define rep(i, v)       for(int i=0; i<sz(v); ++i)
#define lp(i, n)        for(int i=0; i<(int)(n); ++i)
#define lpi(i, j, n)    for(int i=(j); i<(int)(n); ++i)
#define lpd(i, j, n)    for(int i=(j); i>=(int)(n); --i)
//#define INF             1000000000
//#define INF             100000000000000

typedef long long ll;
typedef pair<int, int> ii;
typedef vector<ii> vii;
typedef vector<int> vi;

void init(){
	std::ios_base::sync_with_stdio(false);
	cin.tie(NULL);
	cout.tie(NULL);
}

void swap(int & x, int & y){
	int tmp = x;
	x = y;
	y = tmp;
}

int main()
{
	int t;
	cin >> t;
	while(t--){
		ll x, y, a, b;
		ll ans = 1, l = 1, r = 1e9+5;
		cin >> x >> y >> a >> b;
		if((a == b && x != y) || (a == 0 && x > 0))
			cout << "-1\n";
		else if(a == 0 || (a == b && x == y)){
			cout << "0\n";
		}
		else{
			while(l < r){
				ans = (r-l)/2 + l;
				if(ans * a >= x && ans * b >= y && ans*a - x <= ans*b - y)
					r = ans;
				else
					l = ans+1;
			}
//			if(l > 1e9)
//				cout << "-1\n";
//			else{
				ans = r*b - y;
				cout << ans << "\n";
//			}
		}
	}
	return 0;
}


#include <bits/stdc++.h>

using namespace std;

#define sz(v)           ((int)((v).size()))
#define clr(v, d)       memset(v, d, sizeof(v))
#define rep(i, v)       for(int i=0; i<sz(v); ++i)
#define lp(i, n)        for(int i=0; i<(int)(n); ++i)
#define lpi(i, j, n)    for(int i=(j); i<(int)(n); ++i)
#define lpd(i, j, n)    for(int i=(j); i>=(int)(n); --i)
//#define INF             1000000000
//#define INF             100000000000000

typedef long long ll;
typedef pair<int, int> ii;
typedef vector<ii> vii;
typedef vector<int> vi;

void init(){
	std::ios_base::sync_with_stdio(false);
	cin.tie(NULL);
	cout.tie(NULL);
}

void swap(int & x, int & y){
	int tmp = x;
	x = y;
	y = tmp;
}

int main()
{
	int t;
	cin >> t;
	while(t--){
		ll x, y, a, b;
		ll ans = 1, l = 1, r = 1e9+5;
		cin >> x >> y >> a >> b;
		if((a == b && b == 1) || (a == 0 && x > 0))
			cout << "-1\n";
		else if(a == 0){
			cout << "0\n";
		}
		else{
			while(l < r){
				ans = (r-l)/2 + l;
				if(ans * a >= x && ans * b >= y && ans*a - x <= ans*b - y)
					r = ans;
				else
					l = ans+1;
			}
//			if(l > 1e9)
//				cout << "-1\n";
//			else{
				ans = r*b - y;
				cout << ans << "\n";
//			}
		}
	}
	return 0;
}


#include<bits/stdc++.h>
using namespace std;
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
using namespace __gnu_pbds;
template<class T> using oset=tree<T, null_type, less<T>, rb_tree_tag, tree_order_statistics_node_update>;
#define pb push_back
#define N 200001
#define ll long long
#define int ll
#define pi pair<int , int>
#define pip pair<pair ,int>
#define mp make_pair
#define f first
#define s second
#define mod 998244353
ll Mod(ll x, ll y, int p)
{ll res = 1; x = x % p;
while (y > 0)
{if(y & 1) res = (res*x) % p;
y = y>>1;x = (x*x) % p;
}return res;
}
int* getlps(string pattern){
 int len = pattern.length();
 int * lps = new int[len];
 lps[0] = 0;
 int i = 1 , j = 0;
 while(i < len){
  if(pattern[i] == pattern[j]){
	  lps[i] = j+1;
   i++;
   j++;
  }
  else{
		if(j != 0){
		 j = lps[j-1];
		}
		else{
		 lps[i] = 0;
		 i++;
		}
  }
 }
 return lps;
}
class Triplet{
 public:
 int x ;
 int y ;
 int gcd;
};
Triplet extendedEuclid(int a , int b){
 if(b == 0){Triplet ans; ans.gcd = a;ans.x = 1;ans.y = 0;return ans;}
 Triplet smallAns = extendedEuclid(b , a%b);
 Triplet ans;
 ans.gcd = smallAns.gcd;
 ans.x = smallAns.y;
 ans.y = smallAns.x - (a/b)*smallAns.y;
 return ans;
}
int mmInverse(int a , int m){
 Triplet ans = extendedEuclid(a , m);
 return ans.x;
}
int x , y , p , q;
bool check(int mul){
	if(mul*p - x < 0 || mul*q - y < 0)
	return false;

	return (mul*p - x <= mul*q - y);
}
int32_t main(){
ios_base::sync_with_stdio(false);
cin.tie(NULL);
int t;
cin>>t;
while(t--){
	cin>>x>>y>>p>>q;
int l = 1 , r = 1e9;
int ans =LLONG_MAX;
while(l <= r){
 int mid = (l+r)/2;
 if(check(mid)){
    ans = mid;
    r = mid-1;
 }
 else{
    l = mid+1;
 }
}
 if(ans == LLONG_MAX){
  cout<<"-1"<<endl;
  continue;
 }
 cout<<ans*q-y<<endl;

}
}


#include<bits/stdc++.h>
using namespace std;
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
using namespace __gnu_pbds;
template<class T> using oset=tree<T, null_type, less<T>, rb_tree_tag, tree_order_statistics_node_update>;
#define pb push_back
#define N 200001
#define ll long long
#define int ll
#define pi pair<int , int>
#define pip pair<pair ,int>
#define mp make_pair
#define f first
#define s second
#define mod 998244353
ll Mod(ll x, ll y, int p)
{ll res = 1; x = x % p;
while (y > 0)
{if(y & 1) res = (res*x) % p;
y = y>>1;x = (x*x) % p;
}return res;
}
int* getlps(string pattern){
 int len = pattern.length();
 int * lps = new int[len];
 lps[0] = 0;
 int i = 1 , j = 0;
 while(i < len){
  if(pattern[i] == pattern[j]){
	  lps[i] = j+1;
   i++;
   j++;
  }
  else{
		if(j != 0){
		 j = lps[j-1];
		}
		else{
		 lps[i] = 0;
		 i++;
		}
  }
 }
 return lps;
}
class Triplet{
 public:
 int x ;
 int y ;
 int gcd;
};
Triplet extendedEuclid(int a , int b){
 if(b == 0){Triplet ans; ans.gcd = a;ans.x = 1;ans.y = 0;return ans;}
 Triplet smallAns = extendedEuclid(b , a%b);
 Triplet ans;
 ans.gcd = smallAns.gcd;
 ans.x = smallAns.y;
 ans.y = smallAns.x - (a/b)*smallAns.y;
 return ans;
}
int mmInverse(int a , int m){
 Triplet ans = extendedEuclid(a , m);
 return ans.x;
}
int x , y , p , q;
bool check(int mul){
	if(mul*p - x < 0 || mul*q - y < 0)
	return false;

	return (mul*p - x <= mul*q - y);
}
int32_t main(){
ios_base::sync_with_stdio(false);
cin.tie(NULL);
int t;
cin>>t;
while(t--){
	cin>>x>>y>>p>>q;
int l = 1 , r = 1e8;
int ans =INT_MAX;
while(l <= r){
 int mid = (l+r)/2;
 if(check(mid)){
    ans = mid;
    r = mid-1;
 }
 else{
    l = mid+1;
 }
}
 if(ans == INT_MAX){
  cout<<"-1"<<endl;
  continue;
 }
 cout<<ans*q-y<<endl;

}
}


#include<bits/stdc++.h>
using namespace std;
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
using namespace __gnu_pbds;
template<class T> using oset=tree<T, null_type, less<T>, rb_tree_tag, tree_order_statistics_node_update>;
#define pb push_back
#define N 200001
#define ll long long
#define int ll
#define pi pair<int , int>
#define pip pair<pair ,int>
#define mp make_pair
#define f first
#define s second
#define mod 998244353
ll Mod(ll x, ll y, int p)
{ll res = 1; x = x % p;
while (y > 0)
{if(y & 1) res = (res*x) % p;
y = y>>1;x = (x*x) % p;
}return res;
}
int* getlps(string pattern){
 int len = pattern.length();
 int * lps = new int[len];
 lps[0] = 0;
 int i = 1 , j = 0;
 while(i < len){
  if(pattern[i] == pattern[j]){
	  lps[i] = j+1;
   i++;
   j++;
  }
  else{
		if(j != 0){
		 j = lps[j-1];
		}
		else{
		 lps[i] = 0;
		 i++;
		}
  }
 }
 return lps;
}
class Triplet{
 public:
 int x ;
 int y ;
 int gcd;
};
Triplet extendedEuclid(int a , int b){
 if(b == 0){Triplet ans; ans.gcd = a;ans.x = 1;ans.y = 0;return ans;}
 Triplet smallAns = extendedEuclid(b , a%b);
 Triplet ans;
 ans.gcd = smallAns.gcd;
 ans.x = smallAns.y;
 ans.y = smallAns.x - (a/b)*smallAns.y;
 return ans;
}
int mmInverse(int a , int m){
 Triplet ans = extendedEuclid(a , m);
 return ans.x;
}
int x , y , p , q;
bool check(int mul){
	if(q - p == 0 && y - x == 0)return true;
	else if(q-p == 0)return false;
	// mul*p - x < 0
	if(p < (x+mul-1)/mul || q < (y+mul-1)/mul)
	return false;
	// mul*p - x && mul*q - y >= mul*p -x
	// mul*(q-p)>= y-x;
	return (mul >= ((y-x)+(q-p) - 1)/(q-p));
}
int32_t main(){
ios_base::sync_with_stdio(false);
cin.tie(NULL);
int t;
cin>>t;
while(t--){
	cin>>x>>y>>p>>q;
int l = 1 , r = 1e17;
int ans =LLONG_MAX;
while(l <= r){
 int mid = (l+r)/2;
 if(check(mid)){
    ans = (int)mid;
    r = mid-1;
 }
 else{
    l = mid+1;
 }
}
 if(ans == LLONG_MAX){
  cout<<"-1"<<endl;
  continue;
 }
 cout<<ans*q-y<<endl;

}
}


#include<bits/stdc++.h>
using namespace std;
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
using namespace __gnu_pbds;
template<class T> using oset=tree<T, null_type, less<T>, rb_tree_tag, tree_order_statistics_node_update>;
#define pb push_back
#define N 200001
#define ll long long
#define int ll
#define pi pair<int , int>
#define pip pair<pair ,int>
#define mp make_pair
#define f first
#define s second
#define mod 998244353
ll Mod(ll x, ll y, int p)
{ll res = 1; x = x % p;
while (y > 0)
{if(y & 1) res = (res*x) % p;
y = y>>1;x = (x*x) % p;
}return res;
}
int* getlps(string pattern){
 int len = pattern.length();
 int * lps = new int[len];
 lps[0] = 0;
 int i = 1 , j = 0;
 while(i < len){
  if(pattern[i] == pattern[j]){
	  lps[i] = j+1;
   i++;
   j++;
  }
  else{
		if(j != 0){
		 j = lps[j-1];
		}
		else{
		 lps[i] = 0;
		 i++;
		}
  }
 }
 return lps;
}
class Triplet{
 public:
 int x ;
 int y ;
 int gcd;
};
Triplet extendedEuclid(int a , int b){
 if(b == 0){Triplet ans; ans.gcd = a;ans.x = 1;ans.y = 0;return ans;}
 Triplet smallAns = extendedEuclid(b , a%b);
 Triplet ans;
 ans.gcd = smallAns.gcd;
 ans.x = smallAns.y;
 ans.y = smallAns.x - (a/b)*smallAns.y;
 return ans;
}
int mmInverse(int a , int m){
 Triplet ans = extendedEuclid(a , m);
 return ans.x;
}
double x , y , p , q;
bool check(double mul){
	if(q - p == 0 && y - x == 0)return true;
	else if(q-p == 0)return false;
	// mul*p - x < 0
	if(p < x/mul || q < y/mul)
	return false;
	// mul*p - x && mul*q - y >= mul*p -x
	// mul*(q-p)>= y-x;
	return (mul >= (y-x)/(q-p));
}
int32_t main(){
ios_base::sync_with_stdio(false);
cin.tie(NULL);
int t;
cin>>t;
while(t--){
	cin>>x>>y>>p>>q;
int l = 1 , r = 1e17;
int ans =LLONG_MAX;
while(l <= r){
 int mid = (l+r)/2;
 if(check(mid)){
    ans = mid;
    r = mid-1;
 }
 else{
    l = mid+1;
 }
}
 if(ans == LLONG_MAX){
  cout<<"-1"<<endl;
  continue;
 }
 cout<<ans*q-y<<endl;

}
}


#include <iostream>
#include <bits/stdc++.h>

using namespace std;
#define ll long long // chnage here

ll x,y,p,q;
ll f(ll a)
{

    ll num=(a*p-x);
    ll denum=(a*q-y);
    if(denum<0||num<0)
    {
        return 0;
    }
    if(denum>=num)
    {
        return 1;
    }
    else
    {
        return 0;
    }
}
int main()
{
    //cout << "Hello world!" << endl;
    ll i,j ,k,t;
    cin>>t;
    while(t--)
    {
        cin>>x>>y>>p>>q;

        if((double)x/y==(double)p/q)
        {
            cout<<"0\n";
            continue;
        }
        if((p>=q&&x!=y)||(p==0&&q==1))
        {
            cout<<"-1\n";
            continue;
        }
        ll lo=(x/p),hi=10e9;
    ll mid;
    ll last=-1;
    while(hi-lo>1)
    {

        mid=lo+(hi-lo)/2;
      //  printf("lo:%ld mid:%ld hi:%ld \n",lo,mid,hi);
        if(f(mid))
        {
            last=mid;
            hi=mid;
        }
        else
        {
            lo=mid+1;
        }
    }
    ll  ans=(q*last-y);
    cout<<ans<<"\n";
    }

    return 0;
}


#include <iostream>
#include <bits/stdc++.h>

using namespace std;
#define ll long // chnage here

ll x,y,p,q;
ll f(ll a)
{

    ll num=(a*p-x);
    ll denum=(a*q-y);
    if(denum<0||num<0)
    {
        return 0;
    }
    if(denum>=num)
    {
        return 1;
    }
    else
    {
        return 0;
    }
}
int main()
{
    //cout << "Hello world!" << endl;
    ll i,j ,k,t;
    cin>>t;
    while(t--)
    {
        cin>>x>>y>>p>>q;
        if(x*q==y*p)
        {
            cout<<"0\n";
            continue;
        }
        if(p>=q&&x!=y)
        {
            cout<<"-1\n";
            continue;
        }
        ll lo=(x/p),hi=1000;
    ll mid;
    ll last=-1;
    while(lo<hi)
    {

        mid=lo+(hi-lo)/2;
      //  printf("lo:%ld mid:%ld hi:%ld \n",lo,mid,hi);
        if(f(mid))
        {
            last=mid;
            hi=mid;
        }
        else
        {
            lo=mid+1;
        }
    }
ll    ans=(q*last-y);
    cout<<ans<<"\n";
    }

    return 0;
}


#include <iostream>
#include <bits/stdc++.h>

using namespace std;
#define ll long long // chnage here

ll x,y,p,q;
ll f(ll a)
{

    ll num=(a*p-x);
    ll denum=(a*q-y);
    if(denum<0||num<0)
    {
        return 0;
    }
    if(denum>=num)
    {
        return 1;
    }
    else
    {
        return 0;
    }
}
int main()
{
    //cout << "Hello world!" << endl;
    ll i,j ,k,t;
    cin>>t;
    while(t--)
    {
        cin>>x>>y>>p>>q;
        if(x*q==y*p)
        {
            cout<<"0\n";
            continue;
        }
        if(p>=q&&x!=y)
        {
            cout<<"-1\n";
            continue;
        }
        ll lo=(x/p),hi=1000;
    ll mid;
    ll last=-1;
    while(lo<hi)
    {

        mid=lo+(hi-lo)/2;
      //  printf("lo:%ld mid:%ld hi:%ld \n",lo,mid,hi);
        if(f(mid))
        {
            last=mid;
            hi=mid;
        }
        else
        {
            lo=mid+1;
        }
    }
ll    ans=(q*last-y);
    cout<<ans<<"\n";
    }

    return 0;
}


#include <iostream>
#include <bits/stdc++.h>

using namespace std;
#define ll long long // chnage here

ll x,y,p,q;
ll f(ll a)
{

    ll num=(a*p-x);
    ll denum=(a*q-y);
    if(denum<0||num<0)
    {
        return 0;
    }
    if(denum>=num)
    {
        return 1;
    }
    else
    {
        return 0;
    }
}
int main()
{
    //cout << "Hello world!" << endl;
    ll i,j ,k,t;
    cin>>t;
    while(t--)
    {
        cin>>x>>y>>p>>q;

        if((double)x/y==(double)p/q)
        {
            cout<<"0\n";
            continue;
        }
        if((p>=q&&x!=y)||(p==0&&q==1))
        {
            cout<<"-1\n";
            continue;
        }
        ll lo=(x/p),hi=10e9;
    ll mid;
    ll last=-1;
    while(lo<hi)
    {

        mid=lo+(hi-lo)/2;
      //  printf("lo:%ld mid:%ld hi:%ld \n",lo,mid,hi);
        if(f(mid))
        {
            last=mid;
            hi=mid;
        }
        else
        {
            lo=mid+1;
        }
    }
    ll  ans=(q*last-y);
    cout<<ans<<"\n";
    }

    return 0;
}


#include <iostream>
#include <bits/stdc++.h>

using namespace std;
#define ll long long // chnage here

ll x,y,p,q;
ll f(ll a)
{

    ll num=(a*p-x);
    ll denum=(a*q-y);
    if(denum<0||num<0)
    {
        return 0;
    }
    if(denum>=num)
    {
        return 1;
    }
    else
    {
        return 0;
    }
}
int main()
{
    //cout << "Hello world!" << endl;
    ll i,j ,k,t;
    cin>>t;
    while(t--)
    {
        cin>>x>>y>>p>>q;

        if((double)x/y==(double)p/q)
        {
            cout<<"0\n";
            continue;
        }
        if(p>=q&&x!=y)
        {
            cout<<"-1\n";
            continue;
        }
        ll lo=(x/p),hi=10e9;
    ll mid;
    ll last=-1;
    while(hi-lo>1)
    {

        mid=lo+(hi-lo)/2;
      //  printf("lo:%ld mid:%ld hi:%ld \n",lo,mid,hi);
        if(f(mid))
        {
            last=mid;
            hi=mid;
        }
        else
        {
            lo=mid+1;
        }
    }
    ll  ans=(q*last-y);
    cout<<ans<<"\n";
    }

    return 0;
}


#include <iostream>
#include <bits/stdc++.h>

using namespace std;
#define ll long long // chnage here

ll x,y,p,q;
ll f(ll a)
{

    ll num=(a*p-x);
    ll denum=(a*q-y);
    if(denum<0||num<0)
    {
        return 0;
    }
    if(denum>=num)
    {
        return 1;
    }
    else
    {
        return 0;
    }
}
int main()
{
    //cout << "Hello world!" << endl;
    ll i,j ,k,t;
    cin>>t;
    while(t--)
    {
        cin>>x>>y>>p>>q;
        if(x*q==y*p)
        {
            cout<<"0\n";
            continue;
        }
        if(p>=q&&x!=y)
        {
            cout<<"-1\n";
            continue;
        }
        ll lo=(x/p),hi=10e9;
    ll mid;
    ll last=-1;
    while(lo<hi)
    {

        mid=lo+(hi-lo)/2;
      //  printf("lo:%ld mid:%ld hi:%ld \n",lo,mid,hi);
        if(f(mid))
        {
            last=mid;
            hi=mid;
        }
        else
        {
            lo=mid+1;
        }
    }
ll    ans=(q*last-y);
    cout<<ans<<"\n";
    }

    return 0;
}


#include <iostream>
#include <bits/stdc++.h>

using namespace std;
#define ll long long // chnage here

ll x,y,p,q;
ll f(ll a)
{

    ll num=(a*p-x);
    ll denum=(a*q-y);
    if(denum<0||num<0)
    {
        return 0;
    }
    if(denum>=num)
    {
        return 1;
    }
    else
    {
        return 0;
    }
}
int main()
{
    //cout << "Hello world!" << endl;
    ll i,j ,k,t;
    cin>>t;
    while(t--)
    {
        cin>>x>>y>>p>>q;

        if((double)x/y==(double)p/q)
        {
            cout<<"0\n";
            continue;
        }
        if(p>=q&&x!=y)
        {
            cout<<"-1\n";
            continue;
        }
        ll lo=(x/p),hi=10e9;
    ll mid;
    ll last=-1;
    while(lo<hi)
    {

        mid=lo+(hi-lo)/2;
      //  printf("lo:%ld mid:%ld hi:%ld \n",lo,mid,hi);
        if(f(mid))
        {
            last=mid;
            hi=mid;
        }
        else
        {
            lo=mid+1;
        }
    }
    ll  ans=(q*last-y);
    cout<<ans<<"\n";
    }

    return 0;
}


#include <bits/stdc++.h>
#ifdef JLOCAL
#include "stress.h"
#endif
using namespace std;

#define rep(i, z, n) for (int i = (z); i < int(n); ++i)
#define repr(i, n, z) for (int i = int(n) - 1; i >= (z); --i)
#define shl(n) (1 << (n))
#define hbit(n, i) (((n) >> (i)) & 1)

#define STRESS 0
using lint = long long;

void solve(istream& cin, ostream& cout) {
    int t;
    cin >> t;
    rep(it, 0, t) {
        lint x, y, p, q;
        cin >> x >> y >> p >> q;
        if (p == q) {
            if (x != y) {
                cout << "-1\n";
            } else {
                cout << "0\n";
            }
            continue;
        }
        if (x * q == p * y) {
            cout << "0\n";
            continue;
        }
        if (p == 0) {
            cout << "-1\n";
            continue;
        }
        lint k = max((y - x + q - p + 1) / (y - x), (y + q - 1) / q);
        k = max(k, (x + p - 1) / p);
        k = max(k - 1, 1LL);
        for (;; k++) {
            lint s = k * p - x;
            lint t = k * q - y;
            if (t > 0 && s >= 0 && s <= t) {
                cout << t << "\n";
                break;
            }
        }
    }
}

int main() {
#if !defined(JLOCAL) || !STRESS
#ifdef JLOCAL
    freopen("input.txt", "r", stdin);
#endif
    ios_base::sync_with_stdio(false); cin.tie(0); cout.tie(0);
    solve(cin, cout);
#else
    srand();
    for (int ti = 0; ti < 100; ti++) {
        stress::gen();
        stress::stupid();
        ifstream in("input.txt");
        ofstream out("output.txt");
        solve(in, out);
        out.flush();
        stress::check();
    }
    cout << "ok" << endl;
#endif
    return 0;
}


#include <bits/stdc++.h>
#ifdef JLOCAL
#include "stress.h"
#endif
using namespace std;

#define rep(i, z, n) for (int i = (z); i < int(n); ++i)
#define repr(i, n, z) for (int i = int(n) - 1; i >= (z); --i)
#define shl(n) (1 << (n))
#define hbit(n, i) (((n) >> (i)) & 1)

#define STRESS 0
using lint = long long;

void solve(istream& cin, ostream& cout) {
    int t;
    cin >> t;
    rep(it, 0, t) {
        lint x, y, p, q;
        cin >> x >> y >> p >> q;
        if (p == q) {
            if (x != y) {
                cout << "-1\n";
            } else {
                cout << "0\n";
            }
            continue;
        }
        if (x * q == p * y) {
            cout << "0\n";
            continue;
        }
        lint k = max((y - x + q - p + 1) / (y - x), (y + q - 1) / q);
        k = max(k, (x + p - 1) / p);
        k = max(k - 1, 1LL);
        for (;; k++) {
            lint s = k * p - x;
            lint t = k * q - y;
            if (t > 0 && s >= 0 && s <= t) {
                cout << t << "\n";
                break;
            }
        }
    }
}

int main() {
#if !defined(JLOCAL) || !STRESS
#ifdef JLOCAL
    freopen("input.txt", "r", stdin);
#endif
    ios_base::sync_with_stdio(false); cin.tie(0); cout.tie(0);
    solve(cin, cout);
#else
    srand();
    for (int ti = 0; ti < 100; ti++) {
        stress::gen();
        stress::stupid();
        ifstream in("input.txt");
        ofstream out("output.txt");
        solve(in, out);
        out.flush();
        stress::check();
    }
    cout << "ok" << endl;
#endif
    return 0;
}


#include <bits/stdc++.h>
#ifdef JLOCAL
#include "stress.h"
#endif
using namespace std;

#define rep(i, z, n) for (int i = (z); i < int(n); ++i)
#define repr(i, n, z) for (int i = int(n) - 1; i >= (z); --i)
#define shl(n) (1 << (n))
#define hbit(n, i) (((n) >> (i)) & 1)

#define STRESS 0
using lint = long long;

void solve(istream& cin, ostream& cout) {
    int t;
    cin >> t;
    rep(it, 0, t) {
        lint x, y, p, q;
        cin >> x >> y >> p >> q;
        if (p == q) {
            if (x != y) {
                cout << "-1\n";
            } else {
                cout << "0\n";
            }
            continue;
        }
        if (x * q == p * y) {
            cout << "0\n";
            continue;
        }
        if (p == 0) {
            cout << "-1\n";
            continue;
        }
        lint k = max((y - x + q - p - 1) / (q - p), (y + q - 1) / q);
        k = max(k, (x + p - 1) / p);
        k = max(k - 1, 1LL);
        for (;; k++) {
            lint s = k * p - x;
            lint t = k * q - y;
            if (t > 0 && s >= 0 && s <= t) {
                cout << t << "\n";
                break;
            }
        }
    }
}

int main() {
#if !defined(JLOCAL) || !STRESS
#ifdef JLOCAL
    freopen("input.txt", "r", stdin);
#endif
    ios_base::sync_with_stdio(false); cin.tie(0); cout.tie(0);
    solve(cin, cout);
#else
    srand();
    for (int ti = 0; ti < 100; ti++) {
        stress::gen();
        stress::stupid();
        ifstream in("input.txt");
        ofstream out("output.txt");
        solve(in, out);
        out.flush();
        stress::check();
    }
    cout << "ok" << endl;
#endif
    return 0;
}


//By Vasu Agrawal
#include<iostream>
#include<bits/stdc++.h>
typedef long long int ll;
#define pb push_back
#define pob pop_back
#define mk make_pair
#define fr first
#define sc second
#define pu push
#define ins insert
#define mod 1000000007
#define mem(a,val) memset(a,val,sizeof(a))
#define V(a) vector<a>
#define pp pair<ll,ll>
#define lb(a,n) lower_bound(a.begin(),a.end(),n)-a.begin()
#define ub(a,n) upper_bound(a.begin(),a.end(),n)-a.begin()
#define F(i,n) for(ll i=0;i<n;i++)
#define in(a,n) F(i,n) cin>>a[i]
#define in1(a,n) f(i,n) cin>>a[i]
#define out1(a,n); f(i,n) cout<<a[i]<<" ";cout<<endl;
#define out(a,n); F(i,n) cout<<a[i]<<" ";cout<<endl;
#define f(i,n) for(ll i=1;i<=n;i++)
#define vasu2907 ios_base::sync_with_stdio(false); cin.tie(NULL); cout.tie(NULL);
using namespace std;
bool isprime(ll n) //Time Complexity--->sqrt(n)
{   if (n <= 1)  return false; if (n <= 3)  return true;
  	if (n%2 == 0 || n%3 == 0) return false;
    for (int i=5; i*i<=n; i=i+6) if (n%i == 0 || n%(i+2) == 0) return false;
    return true;
}
vector<ll> prime;
void sieve(ll n)
{	bool bakh[n+1];memset(bakh, true, sizeof(bakh));
    for (ll p=2; p*p<=n; p++)
    {	if (bakh[p] == true)
        {	for (ll i=p*p; i<=n; i += p)    bakh[i] = false; } }
  	for (ll p=2; p<=n; p++)    if (bakh[p])
	  	prime.pb(p);
}//Taken from https://www.geeksforgeeks.org/eulers-totient-function-for-all-numbers-smaller-than-or-equal-to-n/
ll eulertotient(ll z)   //Time--->n/logn
{	ll fac=z;
	for(ll i=0;prime[i]*prime[i]<=z;i++)
	{	if(z%prime[i]==0)
		{	fac-=(fac/prime[i]);	while(z%prime[i]==0)	z/=prime[i];	}
	}	if(z>1)	fac-=(fac/z);
	return fac;
}
//Taken from https://www.geeksforgeeks.org/modular-exponentiation-power-in-modular-arithmetic/
ll power(ll x,ll y,ll p)
{	ll res = 1; x = x % p;	while (y > 0)  {	if (y & 1)	res = (res*x) % p; 	y = y>>1;	x = (x*x) % p; }return res; }
//Taken from https://www.geeksforgeeks.org/euclidean-algorithms-basic-and-extended/
ll gcd(ll a,ll b)
{	if (a == 0)    return b; 	return gcd(b % a, a); }
ll lcm(ll a,ll b)
{	ll g=gcd(a,b); ll ans=(a*b)/g; return ans;	}
//Taken from https://www.geeksforgeeks.org/multiplicative-inverse-under-modulo-m/
ll modInverse(ll a, ll m)
{	ll hvf=gcd(a,m);	if(hvf==1)	return power(a,m-2,m);	return -1;}
void multiply(ll F[2][2], ll M[2][2])
{ ll x =  F[0][0]*M[0][0] + F[0][1]*M[1][0];  ll y =  F[0][0]*M[0][1] + F[0][1]*M[1][1];
  ll z =  F[1][0]*M[0][0] + F[1][1]*M[1][0];  ll w =  F[1][0]*M[0][1] + F[1][1]*M[1][1];
  F[0][0] = x;   F[0][1] = y;   F[1][0] = z;   F[1][1] = w;
}
void powermat(ll F[2][2], ll n)
{	if( n == 0 || n == 1)
    return; ll M[2][2] = {{1,1},{1,0}};   powermat(F, n/2);   multiply(F, F);   if (n%2 != 0)      multiply(F, M);
}
ll fib(ll n)
{ ll F[2][2] = {{1,1},{1,0}};
  if (n == 0) return 0; powermat(F, n-1);   return F[0][0];
}
//ll root(ll x)
//{	while(par[x]!=x)
//	{	par[x]=par[par[x]];
//		x=par[x];
//	}
//	return x;
//}
//void uni(ll x,ll y)
//{	x=root(x);
//	y=root(y);
//	if(val[x]<=val[y])
//		par[y]=par[x];
//	else
//		par[x]=par[y];
//}

//freopen("input.txt", "r", stdin);
//freopen("output.txt", "w", stdout);
// Code Starts Here
int main()
{	vasu2907;
	ll t;
	cin>>t;
	double x,y,p,q;
	ll a,b,low,high,mid,ans;
	while(t--)
	{	cin>>x>>y>>p>>q;
		low=0,high=3e9;
		ans=-1;
		while(low<=high)
		{	mid=(low+high)/2;
			a=mid*p;b=mid*q;
			if(a>=x && b>=y && a-x<=b-y)
				ans = b-y,high=mid-1;
			else
				low=mid+1;
		}
		cout<<ans<<endl;
	}
}


//By Vasu Agrawal
#include<iostream>
#include<bits/stdc++.h>
typedef long long int ll;
#define pb push_back
#define pob pop_back
#define mk make_pair
#define fr first
#define sc second
#define pu push
#define ins insert
#define mod 1000000007
#define mem(a,val) memset(a,val,sizeof(a))
#define V(a) vector<a>
#define pp pair<ll,ll>
#define lb(a,n) lower_bound(a.begin(),a.end(),n)-a.begin()
#define ub(a,n) upper_bound(a.begin(),a.end(),n)-a.begin()
#define F(i,n) for(ll i=0;i<n;i++)
#define in(a,n) F(i,n) cin>>a[i]
#define in1(a,n) f(i,n) cin>>a[i]
#define out1(a,n); f(i,n) cout<<a[i]<<" ";cout<<endl;
#define out(a,n); F(i,n) cout<<a[i]<<" ";cout<<endl;
#define f(i,n) for(ll i=1;i<=n;i++)
#define vasu2907 ios_base::sync_with_stdio(false); cin.tie(NULL); cout.tie(NULL);
using namespace std;
bool isprime(ll n) //Time Complexity--->sqrt(n)
{   if (n <= 1)  return false; if (n <= 3)  return true;
  	if (n%2 == 0 || n%3 == 0) return false;
    for (int i=5; i*i<=n; i=i+6) if (n%i == 0 || n%(i+2) == 0) return false;
    return true;
}
vector<ll> prime;
void sieve(ll n)
{	bool bakh[n+1];memset(bakh, true, sizeof(bakh));
    for (ll p=2; p*p<=n; p++)
    {	if (bakh[p] == true)
        {	for (ll i=p*p; i<=n; i += p)    bakh[i] = false; } }
  	for (ll p=2; p<=n; p++)    if (bakh[p])
	  	prime.pb(p);
}//Taken from https://www.geeksforgeeks.org/eulers-totient-function-for-all-numbers-smaller-than-or-equal-to-n/
ll eulertotient(ll z)   //Time--->n/logn
{	ll fac=z;
	for(ll i=0;prime[i]*prime[i]<=z;i++)
	{	if(z%prime[i]==0)
		{	fac-=(fac/prime[i]);	while(z%prime[i]==0)	z/=prime[i];	}
	}	if(z>1)	fac-=(fac/z);
	return fac;
}
//Taken from https://www.geeksforgeeks.org/modular-exponentiation-power-in-modular-arithmetic/
ll power(ll x,ll y,ll p)
{	ll res = 1; x = x % p;	while (y > 0)  {	if (y & 1)	res = (res*x) % p; 	y = y>>1;	x = (x*x) % p; }return res; }
//Taken from https://www.geeksforgeeks.org/euclidean-algorithms-basic-and-extended/
ll gcd(ll a,ll b)
{	if (a == 0)    return b; 	return gcd(b % a, a); }
ll lcm(ll a,ll b)
{	ll g=gcd(a,b); ll ans=(a*b)/g; return ans;	}
//Taken from https://www.geeksforgeeks.org/multiplicative-inverse-under-modulo-m/
ll modInverse(ll a, ll m)
{	ll hvf=gcd(a,m);	if(hvf==1)	return power(a,m-2,m);	return -1;}
void multiply(ll F[2][2], ll M[2][2])
{ ll x =  F[0][0]*M[0][0] + F[0][1]*M[1][0];  ll y =  F[0][0]*M[0][1] + F[0][1]*M[1][1];
  ll z =  F[1][0]*M[0][0] + F[1][1]*M[1][0];  ll w =  F[1][0]*M[0][1] + F[1][1]*M[1][1];
  F[0][0] = x;   F[0][1] = y;   F[1][0] = z;   F[1][1] = w;
}
void powermat(ll F[2][2], ll n)
{	if( n == 0 || n == 1)
    return; ll M[2][2] = {{1,1},{1,0}};   powermat(F, n/2);   multiply(F, F);   if (n%2 != 0)      multiply(F, M);
}
ll fib(ll n)
{ ll F[2][2] = {{1,1},{1,0}};
  if (n == 0) return 0; powermat(F, n-1);   return F[0][0];
}
//ll root(ll x)
//{	while(par[x]!=x)
//	{	par[x]=par[par[x]];
//		x=par[x];
//	}
//	return x;
//}
//void uni(ll x,ll y)
//{	x=root(x);
//	y=root(y);
//	if(val[x]<=val[y])
//		par[y]=par[x];
//	else
//		par[x]=par[y];
//}

//freopen("input.txt", "r", stdin);
//freopen("output.txt", "w", stdout);
// Code Starts Here
int main()
{	vasu2907;
	ll t;
	cin>>t;
	double x,y,p,q;
	while(t--)
	{	cin>>x>>y>>p>>q;
		if(p==q && x!=y)
		{	cout<<-1<<endl;
			continue;
		}
		double a=x/y;
		double req=p/q;
		ll c=0;
		while(a!=req)
		{	if(a<req)
				x+=1,y+=1;
			else
				y+=1;
			c+=1;
			a=x/y;
		}
		cout<<c<<endl;

	}
}


/*
Change hoes like clothes, I can’t get attached
‘Cause these hoes fire starters like lit matches

— XXXtentacion (Jahseh Onfroy) 1998 - 2018 R.I.P
*/

#include <algorithm>
#include <bitset>
#include <complex>
#include <cstring>
#include <deque>
#include <exception>
#include <fstream>
#include <functional>
#include <iomanip>
#include <iosfwd>
#include <iostream>
#include <istream>
#include <iterator>
#include <limits>
#include <list>
#include <locale>
#include <map>
#include <memory>
#include <new>
#include <numeric>
#include <ostream>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <stdexcept>
#include <streambuf>
#include <string>
#include <vector>
//#include <boost/math/common_factor.hpp>
using namespace std;

#define FOR(i, a, b) for(int i = (a); i < (b); ++i)
#define RFOR(i, b, a) for(int i = (b) - 1; i >= (a); --i)
#define REP(i, N) FOR(i, 0, N)
#define RREP(i, N) RFOR(i, N, 0)
#define FILL(A,value) memset(A,value,sizeof(A))

#define ALL(V) V.begin(), V.end()
#define SZ(V) (int)V.size()
#define pb push_back
#define mp make_pair
#define Pi 3.14159265358979

typedef long long ll;
typedef vector <int> vi;
typedef vector<ll> vl;
typedef pair <int, int> pi;
typedef pair <double, double> pd;

//change these according to prob constraints
const int INF = 1000000000;
const int MAX = 100005;
const int MAX1 = 2007;
const int MAX2 = 4007;
const int LEN = 21;
const int BASE = 1000000000;
const double EPS = 1e-7;
const int MOD = 1234567891;

ll x, y, p, q;

ll solve(ll mid){
	if (p * mid > x && q * mid > y && q * mid - y >= p * mid - x) return q * mid - y;
	return -1;
}

int main(void){
	ios_base::sync_with_stdio(false);
	cin.tie(NULL);
	ifstream in("");
	ofstream out("");
	int t;
	cin >> t;
	while(t--){
		cin >> x >> y >> p >> q;
		if ((double)x / y == (double)p / q){
			cout << 0 << endl;
			continue;
		}
		ll lo = 0, hi = 2e9, mid, res = -1;
		while (lo < hi){
			mid = lo + hi >> 1;
			//cout << lo << " " << mid << " " << hi << endl;
			ll tmp = solve(mid);
			if (tmp != -1) hi = mid, res = tmp;
			else lo = mid + 1;
		}
		cout << res << endl;
	}
	return 0;
}


/*
Change hoes like clothes, I can’t get attached
‘Cause these hoes fire starters like lit matches

— XXXtentacion (Jahseh Onfroy) 1998 - 2018 R.I.P
*/

#include <algorithm>
#include <bitset>
#include <complex>
#include <cstring>
#include <deque>
#include <exception>
#include <fstream>
#include <functional>
#include <iomanip>
#include <iosfwd>
#include <iostream>
#include <istream>
#include <iterator>
#include <limits>
#include <list>
#include <locale>
#include <map>
#include <memory>
#include <new>
#include <numeric>
#include <ostream>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <stdexcept>
#include <streambuf>
#include <string>
#include <vector>
//#include <boost/math/common_factor.hpp>
using namespace std;

#define FOR(i, a, b) for(int i = (a); i < (b); ++i)
#define RFOR(i, b, a) for(int i = (b) - 1; i >= (a); --i)
#define REP(i, N) FOR(i, 0, N)
#define RREP(i, N) RFOR(i, N, 0)
#define FILL(A,value) memset(A,value,sizeof(A))

#define ALL(V) V.begin(), V.end()
#define SZ(V) (int)V.size()
#define pb push_back
#define mp make_pair
#define Pi 3.14159265358979

typedef long long ll;
typedef vector <int> vi;
typedef vector<ll> vl;
typedef pair <int, int> pi;
typedef pair <double, double> pd;

//change these according to prob constraints
const int INF = 1000000000;
const int MAX = 100005;
const int MAX1 = 2007;
const int MAX2 = 4007;
const int LEN = 21;
const int BASE = 1000000000;
const double EPS = 1e-7;
const int MOD = 1234567891;

ll x, y, p, q;

ll solve(ll mid){
	if (p * mid >= x && q * mid >= y && q * mid - y >= p * mid - x) return q * mid - y;
	return -1;
}

int main(void){
	ios_base::sync_with_stdio(false);
	cin.tie(NULL);
	ifstream in("");
	ofstream out("");
	int t;
	cin >> t;
	while(t--){
		cin >> x >> y >> p >> q;
		if ((double)x / y == (double)p / q){
			cout << 0 << endl;
			continue;
		}
		ll lo = 0, hi = 1e9, mid, res = -1;
		while (lo <= hi){
			mid = lo + hi >> 1;
			//cout << lo << " " << mid << " " << hi << endl;
			ll tmp = solve(mid);
			if (tmp != -1) hi = mid - 1, res = tmp;
			else lo = mid + 1;
		}
		cout << res << endl;
	}
	return 0;
}


/*
Change hoes like clothes, I can’t get attached
‘Cause these hoes fire starters like lit matches

— XXXtentacion (Jahseh Onfroy) 1998 - 2018 R.I.P
*/

#include <algorithm>
#include <bitset>
#include <complex>
#include <cstring>
#include <deque>
#include <exception>
#include <fstream>
#include <functional>
#include <iomanip>
#include <iosfwd>
#include <iostream>
#include <istream>
#include <iterator>
#include <limits>
#include <list>
#include <locale>
#include <map>
#include <memory>
#include <new>
#include <numeric>
#include <ostream>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <stdexcept>
#include <streambuf>
#include <string>
#include <vector>
//#include <boost/math/common_factor.hpp>
using namespace std;

#define FOR(i, a, b) for(int i = (a); i < (b); ++i)
#define RFOR(i, b, a) for(int i = (b) - 1; i >= (a); --i)
#define REP(i, N) FOR(i, 0, N)
#define RREP(i, N) RFOR(i, N, 0)
#define FILL(A,value) memset(A,value,sizeof(A))

#define ALL(V) V.begin(), V.end()
#define SZ(V) (int)V.size()
#define pb push_back
#define mp make_pair
#define Pi 3.14159265358979

typedef long long ll;
typedef vector <int> vi;
typedef vector<ll> vl;
typedef pair <int, int> pi;
typedef pair <double, double> pd;

//change these according to prob constraints
const int INF = 1000000000;
const int MAX = 100005;
const int MAX1 = 2007;
const int MAX2 = 4007;
const int LEN = 21;
const int BASE = 1000000000;
const double EPS = 1e-7;
const int MOD = 1234567891;

ll x, y, p, q;

bool solve(ll mid){
	if (p * mid > x && q * mid > y && q * mid - y >= p * mid - x) return 1;
	return 0;
}

int main(void){
	ios_base::sync_with_stdio(false);
	cin.tie(NULL);
	ifstream in("");
	ofstream out("");
	int t;
	cin >> t;
	while(t--){
		cin >> x >> y >> p >> q;
		if ((double)x / y == (double)p / q){
			cout << 0 << endl;
			continue;
		}
		ll lo = 0, hi = 2e9, mid;
		if (solve(hi) == -1){
			cout << -1 << endl;
			continue;
		}
		while (lo + 1 < hi){
			mid = lo + hi >> 1;
			//cout << lo << " " << mid << " " << hi << endl;
			if (solve(mid)) hi = mid;
			else lo = mid + 1;
		}
		cout << lo * q - y << endl;
	}
	return 0;
}


/*
Change hoes like clothes, I can’t get attached
‘Cause these hoes fire starters like lit matches

— XXXtentacion (Jahseh Onfroy) 1998 - 2018 R.I.P
*/

#include <algorithm>
#include <bitset>
#include <complex>
#include <cstring>
#include <deque>
#include <exception>
#include <fstream>
#include <functional>
#include <iomanip>
#include <iosfwd>
#include <iostream>
#include <istream>
#include <iterator>
#include <limits>
#include <list>
#include <locale>
#include <map>
#include <memory>
#include <new>
#include <numeric>
#include <ostream>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <stdexcept>
#include <streambuf>
#include <string>
#include <vector>
//#include <boost/math/common_factor.hpp>
using namespace std;

#define FOR(i, a, b) for(int i = (a); i < (b); ++i)
#define RFOR(i, b, a) for(int i = (b) - 1; i >= (a); --i)
#define REP(i, N) FOR(i, 0, N)
#define RREP(i, N) RFOR(i, N, 0)
#define FILL(A,value) memset(A,value,sizeof(A))

#define ALL(V) V.begin(), V.end()
#define SZ(V) (int)V.size()
#define pb push_back
#define mp make_pair
#define Pi 3.14159265358979

typedef long long ll;
typedef vector <int> vi;
typedef vector<ll> vl;
typedef pair <int, int> pi;
typedef pair <double, double> pd;

//change these according to prob constraints
const int INF = 1000000000;
const int MAX = 100005;
const int MAX1 = 2007;
const int MAX2 = 4007;
const int LEN = 21;
const int BASE = 1000000000;
const double EPS = 1e-7;
const int MOD = 1234567891;

ll x, y, p, q;

bool solve(ll mid){
	if (p * mid > x && q * mid > y && q * mid - y >= p * mid - x) return 1;
	return 0;
}

int main(void){
	ios_base::sync_with_stdio(false);
	cin.tie(NULL);
	ifstream in("");
	ofstream out("");
	int t;
	cin >> t;
	while(t--){
		cin >> x >> y >> p >> q;
		if ((double)x / y == (double)p / q){
			cout << 0 << endl;
			continue;
		}
		ll lo = -1, hi = 1e9, mid;
		if (!solve(hi)){
			cout << -1 << endl;
			continue;
		}
		while (lo + 1 < hi){
			mid = lo + hi >> 1;
			//cout << lo << " " << mid << " " << hi << endl;
			if (solve(mid)) hi = mid;
			else lo = mid + 1;
		}
		cout << lo * q - y << endl;
	}
	return 0;
}


#include<bits/stdc++.h>
using namespace std;
long long t, x, y, p, q;

int main(){
    ios_base::sync_with_stdio(false);
    cin.tie(NULL);

//    freopen("input.txt","r",stdin);
//    freopen("output.txt","w",stdout);

    cin >> t;
    while (t--) {
        cin >> x >> y >> p >> q;
        if (p == 0) {
            if (x == 0) cout << 0 << '\n';
            else cout << -1 << '\n';
            continue;
        }
        if (p == 1 && q == 1) {
            if (x == y) cout << 0 << '\n';
            else cout << -1 << '\n';
            continue;
        }
        int k = max((y - x + q - p - 1) / (q - p), (x + p - 1) / p);
        cout << k * q - y << '\n';
    }
}


#include<bits/stdc++.h>
using namespace std;
int t, x, y, p, q;

int main(){
    ios_base::sync_with_stdio(false);
    cin.tie(NULL);

//    freopen("input.txt","r",stdin);
//    freopen("output.txt","w",stdout);

    cin >> t;
    while (t--) {
        cin >> x >> y >> p >> q;
        if (p == 0) {
            if (x == 0) cout << 0 << '\n';
            else cout << -1 << '\n';
            continue;
        }
        if (p == 1 && q == 1) {
            if (x == y) cout << 0 << '\n';
            else cout << -1 << '\n';
            continue;
        }
        int k = max((y - x + q - p - 1) / (q - p), (x + p - 1) / p);
        cout << k * q - y << '\n';
    }
}


#include<bits/stdc++.h>
using namespace std;
int t, x, y, p, q;

int main(){
    ios_base::sync_with_stdio(false);
    cin.tie(NULL);

//    freopen("input.txt","r",stdin);
//    freopen("output.txt","w",stdout);

    cin >> t;
    while (t--) {
        cin >> x >> y >> p >> q;
        if (p == 1 && q == 1) {
            if (x == y) cout << 0 << '\n';
            else cout << -1 << '\n';
            continue;
        }
        int k = max((y - x + q - p - 1) / (q - p), (x + p - 1) / p);
        cout << k * q - y << '\n';
    }
}


                                    /* Survive. Live. Win. Conquer.*/

#include<bits/stdc++.h>
using namespace std;
typedef long long LL ;
#define get(a) for( int i = 0; i < n; i++) sf("%d", &a[i]) ;
#define bug printf("TRUST\n");
#define pf printf
#define sf scanf
#define lp(i, n) for(int i = 0; i < n; i++)
#define rlp(n, i) for(int i = n - 1; i >= 0; i--)
#define bye vector<LL>
#define pb push_back
#define r0 return 0
#define ff first
#define ss second
#define mp make_pair
#define pii pair <int, int>
#define mod 1000000007
#define nod 200003

void func(void)
{
        freopen("input.c","r",stdin);
        freopen("output.c","w",stdout);
}

int main(){
//    ios_base::sync_with_stdio(0); cin.tie(0);
//    system("COLOR F0");
    LL t;
    cin >> t;
    while(t--){
        LL n, q, i,j = 0, temp, t, k, ans = 0, sum = 0, x, y, p, cnt = 0, m, fg = 0, mx = 0, mx1 = 0, mn = 8000000000000000000, mn1 = 8000000000000000000;
        cin >> x >> y >> p >> q;
        LL gcd = __gcd(x, y);
        x /= gcd;
        y /= gcd;
        if(x == p and y == q){
            cout << "0\n";
            continue;
        }
        if(p == q){
            if(x == y){
                cout << "0\n";
                continue;
            }
            else {
                cout << "-1\n";
                continue;
            }
        }
        x *= gcd;
        y *= gcd;
        if(p == 0){
            if(x == 0){
                cout << "0\n";
                continue;
            }
            else{
                cout << -1 << endl;
                continue;
            }
        }

        LL low = 0, high = 1e9, mid;
        while(low <= high){
            mid = (low + high)/2;
            LL pp = p * mid;
            LL qq = q * mid;
            if(pp - x >= 0 and qq - y >= 0 and pp - x <= qq - y){
                ans = mid;
                high = mid - 1;
            }
            else low = mid + 1;
        }
        cout << ans * q - y << endl;

    }
}


                                    /* Survive. Live. Win. Conquer.*/

#include<bits/stdc++.h>
using namespace std;
typedef long long LL ;
#define get(a) for( int i = 0; i < n; i++) sf("%d", &a[i]) ;
#define bug printf("TRUST\n");
#define pf printf
#define sf scanf
#define lp(i, n) for(int i = 0; i < n; i++)
#define rlp(n, i) for(int i = n - 1; i >= 0; i--)
#define bye vector<LL>
#define pb push_back
#define r0 return 0
#define ff first
#define ss second
#define mp make_pair
#define pii pair <int, int>
#define mod 1000000007
#define nod 200003

void func(void)
{
        freopen("input.c","r",stdin);
        freopen("output.c","w",stdout);
}

int main(){
//    ios_base::sync_with_stdio(0); cin.tie(0);
    system("COLOR F0");
    LL t;
    cin >> t;
    while(t--){
        LL n, q, i,j = 0, temp, t, k, ans = 0, sum = 0, x, y, p, cnt = 0, m, fg = 0, mx = 0, mx1 = 0, mn = 8000000000000000000, mn1 = 8000000000000000000;
        cin >> x >> y >> p >> q;
        LL gcd = __gcd(x, y);
        x /= gcd;
        y /= gcd;
        if(x == p and y == q){
            cout << "0\n";
            continue;
        }
        if(p == q){
            if(x == y){
                cout << "0\n";
                continue;
            }
            else {
                cout << "-1\n";
                continue;
            }
        }
        x *= gcd;
        y *= gcd;
        if(p == 0){
            if(x == 0){
                cout << "0\n";
                continue;
            }
            else{
                cout << -1 << endl;
                continue;
            }
        }

        LL low = 0, high = 1e9, mid;
        while(low <= high){
            mid = (low + high)/2;
            LL pp = p * mid;
            LL qq = q * mid;
            if(pp - x >= 0 and qq - y >= 0 and pp - x <= qq - y){
                ans = mid;
                high = mid - 1;
            }
            else low = mid + 1;
        }
        cout << ans * q - y << endl;

    }
}


#include<bits/stdc++.h>
using namespace std;

#define inf 1001000009
#define infmx 1e18ll
#define ff first
#define ss second
#define ll long long
#define pb push_back
#define IOS ios_base::sync_with_stdio(0);cin.tie(NULL);cout.tie(NULL)
#define all(x) x.begin(),x.end()
///debug
#define wh(x)   cerr<<#x<<" is "<<x<<endl;
#define error(args...){string _s=#args;replace(_s.begin(),_s.end(),',',' ');stringstream _ss(_s);istream_iterator<string> _it(_ss);err(_it,args);}
void err(istream_iterator<string>it){cout<<endl;}
template<class H,class... T>void err(istream_iterator<string>it,H a,T... t){cerr<<*it<<" = "<<a<<" | ";err(++it,t...);}
template<class S,class T>ostream&operator<<(ostream&os,pair<S,T>p){os<<"["<<p.first<<", "<<p.second<<"]";return os;};
template<class T>auto&operator<<(ostream& os,vector<T>_v){bool a=1;for(auto x:_v){os<<(a?"":" ")<<x;a=0;}return os;}
///
typedef pair<int,int>pi;
typedef pair<long long,long long>pll;
typedef pair<pi,int>ppi;
typedef pair<int,pi>pip;
typedef vector<int>vi;
typedef vector<pi>vpi;
const int mod=1e9+7;
ll power(ll a,ll p){ll r=1,y=a;while(p){if(p&1)r=(1LL*r*y)%mod;p>>=1;y=(1LL*y*y)%mod;}return r;}

const int N=2100000;
int ara[N];
ll x,y,p,q;
bool possible(int k)
{
    ll _x=p*k;
    ll _y=q*k;
//    error(_x,_y,_x-x,_y-y);
    if(_x-x<=_y-y&&(_x-x)>=0&&(_y-y)>=0)return 1;
    return 0;
}

int main()
{
    IOS;
    int t;
    cin>>t;
    while(t--)
    {
        cin>>x>>y>>p>>q;
        ll l=0,r=inf;
        while(l<=r)
        {
            ll mid=(l+r)>>1;
            if(possible(mid))
                r=mid-1;
            else l=mid+1;
        }
        if(l>=inf)cout<<-1<<endl;
        else{
            ll _y=q*l;
            ll _x=p*l;
//            error(_x,_y);
            cout<<_y-y<<endl;
        }
    }
}


#include<bits/stdc++.h>
using namespace std;

#define inf 1001000009
#define infmx 1e18ll
#define ff first
#define ss second
#define ll long long
#define pb push_back
#define IOS ios_base::sync_with_stdio(0);cin.tie(NULL);cout.tie(NULL)
#define all(x) x.begin(),x.end()
///debug
#define wh(x)   cerr<<#x<<" is "<<x<<endl;
#define error(args...){string _s=#args;replace(_s.begin(),_s.end(),',',' ');stringstream _ss(_s);istream_iterator<string> _it(_ss);err(_it,args);}
void err(istream_iterator<string>it){cout<<endl;}
template<class H,class... T>void err(istream_iterator<string>it,H a,T... t){cerr<<*it<<" = "<<a<<" | ";err(++it,t...);}
template<class S,class T>ostream&operator<<(ostream&os,pair<S,T>p){os<<"["<<p.first<<", "<<p.second<<"]";return os;};
template<class T>auto&operator<<(ostream& os,vector<T>_v){bool a=1;for(auto x:_v){os<<(a?"":" ")<<x;a=0;}return os;}
///
typedef pair<int,int>pi;
typedef pair<long long,long long>pll;
typedef pair<pi,int>ppi;
typedef pair<int,pi>pip;
typedef vector<int>vi;
typedef vector<pi>vpi;
const int mod=1e9+7;
ll power(ll a,ll p){ll r=1,y=a;while(p){if(p&1)r=(1LL*r*y)%mod;p>>=1;y=(1LL*y*y)%mod;}return r;}

const int N=2100000;
int ara[N];
ll x,y,p,q;
bool possible(int k)
{
    ll _x=p*k;
    ll _y=q*k;
//    error(_x,_y,_x-x,_y-y);
    if(_x-x<=_y-y&&(_x-x)>=0&&(_y-y)>=0)return 1;
    return 0;
}

int main()
{
    IOS;
    int t;
    cin>>t;
    while(t--)
    {
        cin>>x>>y>>p>>q;
        ll l=0,r=1e9;
        while(l<=r)
        {
            ll mid=(l+r)>>1;
            if(possible(mid))
                r=mid-1;
            else l=mid+1;
        }
        if(l>=1e9)cout<<-1<<endl;
        else{
            ll _y=q*l;
            ll _x=p*l;
//            error(_x,_y);
            cout<<_y-y<<endl;
        }
    }
}


#include<bits/stdc++.h>
#define ll long long
#define db double
using namespace std;

ll t,x,y,p,q,ans;
db aim;

bool check(ll e){
	if (e<0) return 0;
	return x*q<=(y+e)*p&&p*(y+e)<=q*(x+e);
}

int main(){
	for (cin>>t;t--;){
		cin>>x>>y>>p>>q;
		if (p==q){
			if (x==y) puts("0");
			else puts("-1");
			continue;
		}
		if (p==0){
			if (x==0) puts("0");
			else puts("-1");
			continue;
		}
		aim=max( ((db)x*q-p*y)/p , ((db)p*y-x*q)/(q-p) );
		ans=aim-0.5; ans=(y+ans+q-1)/q*q-y;
		while (!check(ans)) ans+=q;
		printf("%lld\n",ans);
	}
}


#include<bits/stdc++.h>
#define ll long long
#define db long double
using namespace std;

ll t,x,y,p,q,ans;
db aim;

bool check(ll e){
	if (e<0) return 0;
	return (db)x*q<=0.5+(db)(y+e)*p&&(db)p*(y+e)<=0.5+(db)q*(x+e);
}

int main(){
	for (cin>>t;t--;){
		cin>>x>>y>>p>>q;
		if (p==q){
			if (x==y) puts("0");
			else puts("-1");
			continue;
		}
		if (p==0){
			if (x==0) puts("0");
			else puts("-1");
			continue;
		}
		aim=max( ((db)x*q-p*y)/(db)p , ((db)p*y-x*q)/(db)(q-p) );
		ans=aim-0.5; ans=(y+ans+q-1)/q*q-y;
		while (!check(ans)) ans+=q;
		printf("%lld\n",ans);
	}
}


#include <bits/stdc++.h>
using namespace std;

typedef long long LL;

int main(){
    LL t;
    cin>>t;
    while(t--){
        LL l=0,r=1e9,k,ans=-1;
        LL x,y,p,q,a,b;
        cin>>x>>y>>p>>q;
        for(int i=1;i<=100;i++){
            k=(l+r)/2;
            a=k*p-x;b=k*q-y;
            if(a>=0&&b>=0&&b>=a) {r=k-1,ans=k;}
            else l=k+1;
        }
        if(ans!=-1) cout<<ans*q-y<<endl;
        else cout<<-1<<endl;
    }
    return 0;
}


#include<bits/stdc++.h>

using namespace std;

int main(int argc, char const *argv[])
{
  long t;

  cin >> t;

  long i;

  double x,y,p,q,temp;

  for(i=0;i<t;i++)
  {
    cin >> x >> y >> p >> q;

    if(p==q)
    {
        cout << "-1" << "\n";
        return 0;
    }

   temp= ((p/q)*y-x)/(1-(p/q));

   cout << temp << "\n";

  }

  return 0;
}


#include <bits/stdc++.h>
#define x first
#define y second
#define pb push_back
#define mp make_pair
#define all(v) v.begin(), v.end()

typedef long long ll;
typedef long double ld;

using namespace std;

const int N = 5 * 1e4 + 2;
const int mod = 1e9 + 7;
const int alphabet = 26;
const int dx[] = {1, 1, -1, -1, 2, 2, -2, -2};
const int dy[] = {2, -2, 2, -2, 1, -1, 1, -1};
const int dz[] = {1, 1, 0, 0, -1, 1};

int main() {
  int t;
  cin >> t;
  while(t--) {
    ll x, y, p, q;
    cin >> x >> y >> p >> q;
	ll l = -1;
    ll r = 1e9;
    if(p * r < x || q * r - p * r < y - x){
    	cout << "-1\n";
    	continue;
	}
    while(r - l > 1){
    	ll mid = (l + r) / 2;
    	if(p * mid >= x && q * mid - p * mid >= y - x)r = mid;
    	else l = mid;
	}
	cout << q * r - y << endl;
  }
}


#include <bits/stdc++.h>
#define x first
#define y second
#define pb push_back
#define mp make_pair
#define all(v) v.begin(), v.end()

typedef long long ll;
typedef long double ld;

using namespace std;

const int N = 5 * 1e4 + 2;
const int mod = 1e9 + 7;
const int alphabet = 26;
const int dx[] = {1, 1, -1, -1, 2, 2, -2, -2};
const int dy[] = {2, -2, 2, -2, 1, -1, 1, -1};
const int dz[] = {1, 1, 0, 0, -1, 1};

bool check(int x, int y, int p, int q, int t) {
  if(p * t >= x && q * t - p * t >= y - x)
    return 1;
  else return 0;
}

int main() {
  int t;
  cin >> t;
  while(t--) {
    ll x, y, p, q;
    cin >> x >> y >> p >> q;
    if (p == 0) {
      cout << (x == 0 ? 0 : -1) << endl;
      continue;
    }
    if (p == q) {
      cout << (x == y ? 0 : -1) << endl;
      continue;
    }
    ll l = 0;
    ll r = 1e9;
    while(r - l > 1){
    	ll mid = (l + r) / 2;
    	if(p * mid >= x && q * mid - p * mid >= y - x)r = mid;
    	else l = mid;
	}
	cout << q * r - y << endl;
  }
}


#include <cstdio>
#include <iostream>
#include <vector>
#include <algorithm>
#include <map>
#include <string>
#include <set>
#include <cmath>

using namespace std;

const int MX = 1000000007;

int t;
long long x, y, p , q;

long long check(long long a) {
    if(q * a - y >= a)
        return true;
    return false;
}

int main() {
    cin >> t;
    for(int i = 0;i < t;i++) {
        cin >> x >> y >> p >> q;
        if(p == 0) {
            if(x == 0)
                cout << 0 << endl;
            else
                cout << -1 <<endl;
        }
        if(q == p) {
            if(x == y)
                cout << 0 << endl;
            else
                cout << -1 <<endl;
        } else {

            long long n = (y - x + q - p - 1) / (q - p);
            if(n * p - x < 0)
                n = (x + p - 1) / p;
            cout << (q * n - y) << endl;

        }

    }

}


#include <cstdio>
#include <iostream>
#include <vector>
#include <algorithm>
#include <map>
#include <string>
#include <set>
#include <cmath>

using namespace std;

const int MX = 1000000007;

int t;
long long x, y, p , q;

long long check(long long a) {
    if(q * a - y >= a)
        return true;
    return false;
}

int main() {
    cin >> t;
    for(int i = 0;i < t;i++) {
        cin >> x >> y >> p >> q;
        if(p == 0) {
            if(x == 0)
                cout << 0 << endl;
            else
                cout << -1 <<endl;
        } else if(q == p) {
            if(x == y)
                cout << 0 << endl;
            else
                cout << -1 <<endl;
        } else {

            long long n = (y - x + q - p - 1) / (q - p);
            if(n * p - x < 0)
                n = (x + p - 1) / p;
            cout << (q * n - y) << endl;

        }

    }

}


#include <cstdio>
#include <iostream>
#include <vector>
#include <algorithm>
#include <map>
#include <string>
#include <set>
#include <cmath>

using namespace std;

const int MX = 1000000007;

int t;
long long x, y, p , q;

long long check(long long a) {
    if(q * a - y >= a)
        return true;
    return false;
}

int main() {
    cin >> t;
    for(int i = 0;i < t;i++) {
        cin >> x >> y >> p >> q;
        if(q == p) {
            if(x == y)
                cout << 0 << endl;
            else
                cout << -1 <<endl;
        } else {

            long long n = (y - x + q - p - 1) / (q - p);
            if(n * p - x < 0)
                n = (x + p - 1) / p;
            cout << (q * n - y) << endl;

        }

    }

}


#include <iostream>
#include <vector>
#include <algorithm>
#include <cmath>
using namespace std;

int n,i;
int main()
{
	double x,y,p,q;
	int xx,yy,pp,qq,k;
	cin>>n;
	for(i=0;i<n;++i)
	{
		cin>>x>>y>>p>>q;
		if(!p)
		{
			if(x)
				cout<<"-1"<<endl;
			else
				cout<<"0"<<endl;
			continue;
		}
		if(p==q)
		{
			if(x!=y)
				cout<<"-1"<<endl;
			else
				cout<<"0"<<endl;
			continue;
		}
		k=max(ceil(x/p+0.0),ceil((y-x)/(q-p)+0.0));
		q*=k;
		p*=k;
		xx=x;
		yy=(y-x);
		pp=p;
		qq=(q-p);
		cout<<pp-xx+qq-yy<<endl;
	}
//	system("pause");
	return 0;
}


def func(u,v,a,l):
    if (v**2-u**2)>=2*a*l:
        return ((u**2+2*a*l)**(1/2)-u)/a
    else:
        t1=(v-u)/a
        t2=(l-(u*t1+a*t1*t1/2))/v
        return t1+t2
def efficient(v,a,w,d):
    if 2*v*v-w*w<=2*a*d:
        t1=v/a
        t2=(v-w)/a
        t3=(d-0.5*a*t1*t1-v*t2+0.5*a*t2*t2)/v
        return t1+t2+t3
    else:
        bound=((2*a*d+w*w)/2)**0.5
        t1=bound/a
        t2=(bound-w)/a
        t3=func(0,w,a,d)
        return t1+t2
        return min(t1+t2,t3)
def main():
    string1=input()
    string2=input()
    arr1=string1.split(" ")
    arr2=string2.split(" ")
    a=int(arr1[0])
    v=int(arr1[1])
    l=int(arr2[0])
    d=int(arr2[1])
    w=int(arr2[2])
    if 2*a*d<=w**2 or v<=w:
        t1=func(0,v,a,l)
        print("%.8f"%(t1))
    else:
        t1=efficient(v,a,w,d)
        t2=func(w,v,a,l-d)
        print("%.8f"%(t1+t2))
main()