#include <cassert>
#include <cctype>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <iostream>
#include <sstream>
#include <iomanip>
#include <string>
#include <vector>
#include <deque>
#include <list>
#include <set>
#include <map>
#include <bitset>
#include <stack>
#include <queue>
#include <algorithm>
#include <functional>
#include <iterator>
#include <numeric>
#include <utility>
#include <fstream>
#include <climits>
#include <complex>
#include <new>
#include <memory>
#include <time.h>
//#include <bits/stdc++.h>
using namespace std;

#define pf printf
#define sc scanf
#define ll long long int
#define ull unsigned ll

#define sc1i(a) sc("%d",&a)
#define sc2i(a,b) sc("%d%d",&a,&b)
#define sc3i(a,b,c) sc("%d%d%d",&a,&b,&c)
#define sc4i(a,b,c,d) sc("%d%d%d%d",&a,&b,&c,&d)

#define sc1ll(a) sc("%lld",&a)
#define sc2ll(a,b) sc("%lld%lld",&a,&b)
#define sc3ll(a,b,c) sc("%lld%lld%lld",&a,&b,&c)
#define sc4ll(a,b,c,d) sc("%lld%lld%lld%lld",&a,&b,&c,&d)

#define pb push_back
#define pi acos(-1.0)
#define mem(a,x) memset(a,x,sizeof(a))
#define all(v) v.begin(),v.end()
#define SZ(a) (int)a.size()
#define MP make_pair
#define sqr(a) ((a)*(a))
#define ppcnt(a) __builtin_popcount(a)
#define cnttz(a) __builtin_ctz(a)
#define cntlz(a) __builtin_clz(a)

#define rep(i,k,n) for(__typeof(n)i = k ; i <= n; i++)
#define rrep(i,k,n) for(__typeof(k)i = k ; i >= n; i--)
#define show(a) cerr <<#a<< " = " << a << endl;

#define wait system("pause")
#define ___ ios_base::sync_with_stdio(false);cin.tie(NULL);

//int dx[]= {-1,-1,0,0,1,1};
//int dy[]= {-1,0,-1,1,0,1};
int dx[]= {0,0,1,-1};//4 side move
int dy[]= {-1,1,0,0};//4 side move
//int dx[]= {1,1,0,-1,-1,-1,0,1};//8 side move
//int dy[]= {0,1,1,1,0,-1,-1,-1};//8 side move
//int dx[]={1,1,2,2,-1,-1,-2,-2};//knight move
//int dy[]={2,-2,1,-1,2,-2,1,-1};//knight move

template<class T> T power(T N,T P){ return (P==0)?  1: N*power(N,P-1);}//N^P
template<class T> T gcd(T a,T b){if(b == 0)return a;return gcd(b,a%b);}//gcd(a,b)

struct debugger{
    template<typename T> debugger& operator , (const T& v){
        cerr<< v <<" ";
        return *this;
    }
}dbg;
#define deb(args...) {dbg,args; cerr<<endl;}

void make_unique(vector<ll> &a){ sort(a.begin(), a.end());
    a.erase(unique(a.begin(), a.end()), a.end()); }
//ll bigmod(ll B,ll P,ll M){ ll R=1; while(P>0)  {if(P%2==1){R=(R*B)%M;}P/=2;B=(B*B)%M;} return R;} /// (B^P)%M

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

//int on(int n,int pos){return n=n|(1<<pos);}
//void off(int &n,int pos){ n = n & ~(1<<pos);}
//bool check(int n,int pos){return (bool)(n&(1<<pos));}
//N & (N % 2 ? 0 : ~0) | ( ((N & 2)>>1) ^ (N & 1) )://XOR of 1-n numbers

const int N = 1e6 + 10;
const ll inf = 2e18 ;

ll bs(ll nx , ll ny , ll x , ll y , ll p , ll q){
    ll lo = 0LL , hi = (ll)1e8 , mid , ans = 1e8;
    while(lo <= hi){
        mid = (lo + hi) / 2LL ;
        if( (nx + p*mid - x) <= (ny + q*mid - y) ){
            ans = mid ;
            hi = mid - 1 ;
        }
        else lo = mid + 1;
    }
    return ans ;
}

int main(){
    #ifndef ONLINE_JUDGE
    freopen("input.txt" , "r" , stdin);
    #endif
    int tst ,cas = 0 ;
    ll x , y , p , q , nx , ny , dx , dy ;
    sc1i( tst );
    while(tst--){
        sc4ll( x , y , p  , q) ;

        ll res = inf ;
        ll g = gcd( p , q );
        p /= g ;
        q /= g ;

        ll mx = (((y/q) + ((y % q) > 0 ? 1 :0 ))) ;

        nx = p * mx ;
        ny = q * mx ;

        if(nx < x && nx > 0){
            mx = x / nx ;
            nx += (p * mx);
            ny += (q * mx) ;
            if(nx < x){
                nx += p ;
                ny += q ;
            }
        }

//        deb("nx",nx , "ny" , ny);

        ll k = bs(nx , ny , x , y , p , q ) ;

        nx += p*k ;
        ny += q*k ;

//        deb("nx",nx , "ny" , ny);

        if((nx - x) <= (ny - y)){
            dx = (nx - x) ;
            dy = (ny - y - dx) ;
            res = dx + dy ;
        }
        if(res == inf)pf("-1\n");
        else pf("%lld\n",res) ;
    }
    return 0;
}


#include <cassert>
#include <cctype>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <iostream>
#include <sstream>
#include <iomanip>
#include <string>
#include <vector>
#include <deque>
#include <list>
#include <set>
#include <map>
#include <bitset>
#include <stack>
#include <queue>
#include <algorithm>
#include <functional>
#include <iterator>
#include <numeric>
#include <utility>
#include <fstream>
#include <climits>
#include <complex>
#include <new>
#include <memory>
#include <time.h>
//#include <bits/stdc++.h>
using namespace std;

#define pf printf
#define sc scanf
#define ll long long int
#define ull unsigned ll

#define sc1i(a) sc("%d",&a)
#define sc2i(a,b) sc("%d%d",&a,&b)
#define sc3i(a,b,c) sc("%d%d%d",&a,&b,&c)
#define sc4i(a,b,c,d) sc("%d%d%d%d",&a,&b,&c,&d)

#define sc1ll(a) sc("%lld",&a)
#define sc2ll(a,b) sc("%lld%lld",&a,&b)
#define sc3ll(a,b,c) sc("%lld%lld%lld",&a,&b,&c)
#define sc4ll(a,b,c,d) sc("%lld%lld%lld%lld",&a,&b,&c,&d)

#define pb push_back
#define pi acos(-1.0)
#define mem(a,x) memset(a,x,sizeof(a))
#define all(v) v.begin(),v.end()
#define SZ(a) (int)a.size()
#define MP make_pair
#define sqr(a) ((a)*(a))
#define ppcnt(a) __builtin_popcount(a)
#define cnttz(a) __builtin_ctz(a)
#define cntlz(a) __builtin_clz(a)

#define rep(i,k,n) for(__typeof(n)i = k ; i <= n; i++)
#define rrep(i,k,n) for(__typeof(k)i = k ; i >= n; i--)
#define show(a) cerr <<#a<< " = " << a << endl;

#define wait system("pause")
#define ___ ios_base::sync_with_stdio(false);cin.tie(NULL);

//int dx[]= {-1,-1,0,0,1,1};
//int dy[]= {-1,0,-1,1,0,1};
int dx[]= {0,0,1,-1};//4 side move
int dy[]= {-1,1,0,0};//4 side move
//int dx[]= {1,1,0,-1,-1,-1,0,1};//8 side move
//int dy[]= {0,1,1,1,0,-1,-1,-1};//8 side move
//int dx[]={1,1,2,2,-1,-1,-2,-2};//knight move
//int dy[]={2,-2,1,-1,2,-2,1,-1};//knight move

template<class T> T power(T N,T P){ return (P==0)?  1: N*power(N,P-1);}//N^P
template<class T> T gcd(T a,T b){if(b == 0)return a;return gcd(b,a%b);}//gcd(a,b)

struct debugger{
    template<typename T> debugger& operator , (const T& v){
        cerr<< v <<" ";
        return *this;
    }
}dbg;
#define deb(args...) {dbg,args; cerr<<endl;}

void make_unique(vector<ll> &a){ sort(a.begin(), a.end());
    a.erase(unique(a.begin(), a.end()), a.end()); }
//ll bigmod(ll B,ll P,ll M){ ll R=1; while(P>0)  {if(P%2==1){R=(R*B)%M;}P/=2;B=(B*B)%M;} return R;} /// (B^P)%M

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

//int on(int n,int pos){return n=n|(1<<pos);}
//void off(int &n,int pos){ n = n & ~(1<<pos);}
//bool check(int n,int pos){return (bool)(n&(1<<pos));}
//N & (N % 2 ? 0 : ~0) | ( ((N & 2)>>1) ^ (N & 1) )://XOR of 1-n numbers

const int N = 1e6 + 10;
const ll inf = 2e18 ;

ll bs(ll nx , ll ny , ll x , ll y , ll p , ll q){
    ll lo = 0LL , hi = (ll)2e19 , mid , ans = 2e19;
    while(lo <= hi){
        mid = (lo + hi) / 2LL ;
        if( (nx + p*mid - x) <= (ny + q*mid - y) ){
            ans = mid ;
            hi = mid - 1 ;
        }
        else lo = mid + 1;
    }
    return ans ;
}

int main(){
    #ifndef ONLINE_JUDGE
    freopen("input.txt" , "r" , stdin);
    #endif
    int tst ,cas = 0 ;
    ll x , y , p , q , nx , ny , dx , dy ;
    sc1i( tst );
    while(tst--){
        sc4ll( x , y , p  , q) ;

        ll res = inf ;
        ll g = gcd( p , q );
        p /= g ;
        q /= g ;

        ll mx = (((y/q) + ((y % q) > 0 ? 1 :0 ))) ;

        nx = p * mx ;
        ny = q * mx ;

        if(nx < x){
            mx = x / nx ;
            nx += (p * mx);
            ny += (q * mx) ;
            if(nx < x){
                nx += p ;
                ny += q ;
            }
        }

//        deb("nx",nx , "ny" , ny);

        ll k = bs(nx , ny , x , y , p , q ) ;

        nx += p*k ;
        ny += q*k ;

//        deb("nx",nx , "ny" , ny);

        if((nx - x) <= (ny - y)){
            dx = (nx - x) ;
            dy = (ny - y - dx) ;
            res = dx + dy ;
        }
        if(res == inf)pf("-1\n");
        else pf("%lld\n",res) ;
    }
    return 0;
}


#include <cassert>
#include <cctype>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <iostream>
#include <sstream>
#include <iomanip>
#include <string>
#include <vector>
#include <deque>
#include <list>
#include <set>
#include <map>
#include <bitset>
#include <stack>
#include <queue>
#include <algorithm>
#include <functional>
#include <iterator>
#include <numeric>
#include <utility>
#include <fstream>
#include <climits>
#include <complex>
#include <new>
#include <memory>
#include <time.h>
//#include <bits/stdc++.h>
using namespace std;

#define pf printf
#define sc scanf
#define ll long long int
#define ull unsigned ll

#define sc1i(a) sc("%d",&a)
#define sc2i(a,b) sc("%d%d",&a,&b)
#define sc3i(a,b,c) sc("%d%d%d",&a,&b,&c)
#define sc4i(a,b,c,d) sc("%d%d%d%d",&a,&b,&c,&d)

#define sc1ll(a) sc("%lld",&a)
#define sc2ll(a,b) sc("%lld%lld",&a,&b)
#define sc3ll(a,b,c) sc("%lld%lld%lld",&a,&b,&c)
#define sc4ll(a,b,c,d) sc("%lld%lld%lld%lld",&a,&b,&c,&d)

#define pb push_back
#define pi acos(-1.0)
#define mem(a,x) memset(a,x,sizeof(a))
#define all(v) v.begin(),v.end()
#define SZ(a) (int)a.size()
#define MP make_pair
#define sqr(a) ((a)*(a))
#define ppcnt(a) __builtin_popcount(a)
#define cnttz(a) __builtin_ctz(a)
#define cntlz(a) __builtin_clz(a)

#define rep(i,k,n) for(__typeof(n)i = k ; i <= n; i++)
#define rrep(i,k,n) for(__typeof(k)i = k ; i >= n; i--)
#define show(a) cerr <<#a<< " = " << a << endl;

#define wait system("pause")
#define ___ ios_base::sync_with_stdio(false);cin.tie(NULL);

//int dx[]= {-1,-1,0,0,1,1};
//int dy[]= {-1,0,-1,1,0,1};
int dx[]= {0,0,1,-1};//4 side move
int dy[]= {-1,1,0,0};//4 side move
//int dx[]= {1,1,0,-1,-1,-1,0,1};//8 side move
//int dy[]= {0,1,1,1,0,-1,-1,-1};//8 side move
//int dx[]={1,1,2,2,-1,-1,-2,-2};//knight move
//int dy[]={2,-2,1,-1,2,-2,1,-1};//knight move

template<class T> T power(T N,T P){ return (P==0)?  1: N*power(N,P-1);}//N^P
template<class T> T gcd(T a,T b){if(b == 0)return a;return gcd(b,a%b);}//gcd(a,b)

struct debugger{
    template<typename T> debugger& operator , (const T& v){
        cerr<< v <<" ";
        return *this;
    }
}dbg;
#define deb(args...) {dbg,args; cerr<<endl;}

void make_unique(vector<ll> &a){ sort(a.begin(), a.end());
    a.erase(unique(a.begin(), a.end()), a.end()); }
//ll bigmod(ll B,ll P,ll M){ ll R=1; while(P>0)  {if(P%2==1){R=(R*B)%M;}P/=2;B=(B*B)%M;} return R;} /// (B^P)%M

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

//int on(int n,int pos){return n=n|(1<<pos);}
//void off(int &n,int pos){ n = n & ~(1<<pos);}
//bool check(int n,int pos){return (bool)(n&(1<<pos));}
//N & (N % 2 ? 0 : ~0) | ( ((N & 2)>>1) ^ (N & 1) )://XOR of 1-n numbers

const int N = 1e6 + 10;
const ll inf = 2e18 ;

ll bs(ll nx , ll ny , ll x , ll y , ll p , ll q){
    ll lo = 0LL , hi = (ll)2e9 , mid , ans = 2e9;
    while(lo <= hi){
        mid = (lo + hi) / 2LL ;
        if( (nx + p*mid - x) <= (ny + q*mid - y) ){
            ans = mid ;
            hi = mid - 1 ;
        }
        else lo = mid + 1;
    }
    return ans ;
}

int main(){
    #ifndef ONLINE_JUDGE
    freopen("input.txt" , "r" , stdin);
    #endif
    int tst ,cas = 0 ;
    ll x , y , p , q , nx , ny , dx , dy ;
    sc1i( tst );
    while(tst--){
        sc4ll( x , y , p  , q) ;

        ll res = inf ;
        ll g = gcd( p , q );
        p /= g ;
        q /= g ;

        ll mx = (((y/q) + ((y % q) > 0 ? 1 :0 ))) ;

        nx = p * mx ;
        ny = q * mx ;

        if(nx < x){
            mx = x / nx ;
            nx += (p * mx);
            ny += (q * mx) ;
            if(nx < x){
                nx += p ;
                ny += q ;
            }
        }

//        deb("nx",nx , "ny" , ny);

        ll k = bs(nx , ny , x , y , p , q ) ;

        nx += p*k ;
        ny += q*k ;

//        deb("nx",nx , "ny" , ny);

        if((nx - x) <= (ny - y)){
            dx = (nx - x) ;
            dy = (ny - y - dx) ;
            res = dx + dy ;
        }
        if(res == inf)pf("-1\n");
        else pf("%lld\n",res) ;
    }
    return 0;
}


#include <cassert>
#include <cctype>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <iostream>
#include <sstream>
#include <iomanip>
#include <string>
#include <vector>
#include <deque>
#include <list>
#include <set>
#include <map>
#include <bitset>
#include <stack>
#include <queue>
#include <algorithm>
#include <functional>
#include <iterator>
#include <numeric>
#include <utility>
#include <fstream>
#include <climits>
#include <complex>
#include <new>
#include <memory>
#include <time.h>
//#include <bits/stdc++.h>
using namespace std;

#define pf printf
#define sc scanf
#define ll long long int
#define ull unsigned ll

#define sc1i(a) sc("%d",&a)
#define sc2i(a,b) sc("%d%d",&a,&b)
#define sc3i(a,b,c) sc("%d%d%d",&a,&b,&c)
#define sc4i(a,b,c,d) sc("%d%d%d%d",&a,&b,&c,&d)

#define sc1ll(a) sc("%lld",&a)
#define sc2ll(a,b) sc("%lld%lld",&a,&b)
#define sc3ll(a,b,c) sc("%lld%lld%lld",&a,&b,&c)
#define sc4ll(a,b,c,d) sc("%lld%lld%lld%lld",&a,&b,&c,&d)

#define pb push_back
#define pi acos(-1.0)
#define mem(a,x) memset(a,x,sizeof(a))
#define all(v) v.begin(),v.end()
#define SZ(a) (int)a.size()
#define MP make_pair
#define sqr(a) ((a)*(a))
#define ppcnt(a) __builtin_popcount(a)
#define cnttz(a) __builtin_ctz(a)
#define cntlz(a) __builtin_clz(a)

#define rep(i,k,n) for(__typeof(n)i = k ; i <= n; i++)
#define rrep(i,k,n) for(__typeof(k)i = k ; i >= n; i--)
#define show(a) cerr <<#a<< " = " << a << endl;

#define wait system("pause")
#define ___ ios_base::sync_with_stdio(false);cin.tie(NULL);

//int dx[]= {-1,-1,0,0,1,1};
//int dy[]= {-1,0,-1,1,0,1};
int dx[]= {0,0,1,-1};//4 side move
int dy[]= {-1,1,0,0};//4 side move
//int dx[]= {1,1,0,-1,-1,-1,0,1};//8 side move
//int dy[]= {0,1,1,1,0,-1,-1,-1};//8 side move
//int dx[]={1,1,2,2,-1,-1,-2,-2};//knight move
//int dy[]={2,-2,1,-1,2,-2,1,-1};//knight move

template<class T> T power(T N,T P){ return (P==0)?  1: N*power(N,P-1);}//N^P
template<class T> T gcd(T a,T b){if(b == 0)return a;return gcd(b,a%b);}//gcd(a,b)

struct debugger{
    template<typename T> debugger& operator , (const T& v){
        cerr<< v <<" ";
        return *this;
    }
}dbg;
#define deb(args...) {dbg,args; cerr<<endl;}

void make_unique(vector<ll> &a){ sort(a.begin(), a.end());
    a.erase(unique(a.begin(), a.end()), a.end()); }
//ll bigmod(ll B,ll P,ll M){ ll R=1; while(P>0)  {if(P%2==1){R=(R*B)%M;}P/=2;B=(B*B)%M;} return R;} /// (B^P)%M

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

//int on(int n,int pos){return n=n|(1<<pos);}
//void off(int &n,int pos){ n = n & ~(1<<pos);}
//bool check(int n,int pos){return (bool)(n&(1<<pos));}
//N & (N % 2 ? 0 : ~0) | ( ((N & 2)>>1) ^ (N & 1) )://XOR of 1-n numbers

const int N = 1e6 + 10;
const ll inf = 5e25 ;

int main(){
    #ifndef ONLINE_JUDGE
    freopen("input.txt" , "r" , stdin);
    #endif
    int tst ,cas = 0 ;
    ll x , y , p , q , nx , ny ;
    sc1i( tst );
    while(tst--){
        sc4ll( x , y , p  , q) ;

        ll g = gcd( p , q );
        p /= g ;
        q /= g ;

        ll m = ( y / q) + ((y%q > 0)?1:0) ;
        ll ans , res = inf , dx , dy;

        ny = q * m ;
        nx = p * m ;

        while(ny < y){
            nx += p ;
            ny += q ;
        }

        while(nx < x){
            nx += p ;
            ny += q ;
        }

        if((nx - x) <= (ny - y)){
            ans = 0 ;
            dx = (nx - x) ;
            dy = (ny - y - dx) ;
            ans = dx + dy ;
            res = min( res , ans );
        }

        for(int kk = 0 ; kk < 1e4 ; kk++){
            ny += q ;
            nx += p ;
            if( (nx - x) <= (ny - y)){
                ans = 0 ;
                dx = (nx - x) ;
                dy = (ny - y - dx) ;
                ans = dx + dy ;
                res = min( res , ans );
            }
        }
        if(res == inf)pf("-1\n");
        else pf("%lld\n",res) ;
    }
    return 0;
}


#include <cassert>
#include <cctype>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <iostream>
#include <sstream>
#include <iomanip>
#include <string>
#include <vector>
#include <deque>
#include <list>
#include <set>
#include <map>
#include <bitset>
#include <stack>
#include <queue>
#include <algorithm>
#include <functional>
#include <iterator>
#include <numeric>
#include <utility>
#include <fstream>
#include <climits>
#include <complex>
#include <new>
#include <memory>
#include <time.h>
//#include <bits/stdc++.h>
using namespace std;

#define pf printf
#define sc scanf
#define ll long long int
#define ull unsigned ll

#define sc1i(a) sc("%d",&a)
#define sc2i(a,b) sc("%d%d",&a,&b)
#define sc3i(a,b,c) sc("%d%d%d",&a,&b,&c)
#define sc4i(a,b,c,d) sc("%d%d%d%d",&a,&b,&c,&d)

#define sc1ll(a) sc("%lld",&a)
#define sc2ll(a,b) sc("%lld%lld",&a,&b)
#define sc3ll(a,b,c) sc("%lld%lld%lld",&a,&b,&c)
#define sc4ll(a,b,c,d) sc("%lld%lld%lld%lld",&a,&b,&c,&d)

#define pb push_back
#define pi acos(-1.0)
#define mem(a,x) memset(a,x,sizeof(a))
#define all(v) v.begin(),v.end()
#define SZ(a) (int)a.size()
#define MP make_pair
#define sqr(a) ((a)*(a))
#define ppcnt(a) __builtin_popcount(a)
#define cnttz(a) __builtin_ctz(a)
#define cntlz(a) __builtin_clz(a)

#define rep(i,k,n) for(__typeof(n)i = k ; i <= n; i++)
#define rrep(i,k,n) for(__typeof(k)i = k ; i >= n; i--)
#define show(a) cerr <<#a<< " = " << a << endl;

#define wait system("pause")
#define ___ ios_base::sync_with_stdio(false);cin.tie(NULL);

//int dx[]= {-1,-1,0,0,1,1};
//int dy[]= {-1,0,-1,1,0,1};
int dx[]= {0,0,1,-1};//4 side move
int dy[]= {-1,1,0,0};//4 side move
//int dx[]= {1,1,0,-1,-1,-1,0,1};//8 side move
//int dy[]= {0,1,1,1,0,-1,-1,-1};//8 side move
//int dx[]={1,1,2,2,-1,-1,-2,-2};//knight move
//int dy[]={2,-2,1,-1,2,-2,1,-1};//knight move

template<class T> T power(T N,T P){ return (P==0)?  1: N*power(N,P-1);}//N^P
template<class T> T gcd(T a,T b){if(b == 0)return a;return gcd(b,a%b);}//gcd(a,b)

struct debugger{
    template<typename T> debugger& operator , (const T& v){
        cerr<< v <<" ";
        return *this;
    }
}dbg;
#define deb(args...) {dbg,args; cerr<<endl;}

void make_unique(vector<ll> &a){ sort(a.begin(), a.end());
    a.erase(unique(a.begin(), a.end()), a.end()); }
//ll bigmod(ll B,ll P,ll M){ ll R=1; while(P>0)  {if(P%2==1){R=(R*B)%M;}P/=2;B=(B*B)%M;} return R;} /// (B^P)%M

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

//int on(int n,int pos){return n=n|(1<<pos);}
//void off(int &n,int pos){ n = n & ~(1<<pos);}
//bool check(int n,int pos){return (bool)(n&(1<<pos));}
//N & (N % 2 ? 0 : ~0) | ( ((N & 2)>>1) ^ (N & 1) )://XOR of 1-n numbers

const int N = 1e6 + 10;
const ll inf = 2e18 ;

ll bs(ll nx , ll ny , ll x , ll y , ll p , ll q){
    ll lo = 0LL , hi = (ll)2e10 , mid , ans = 2e10;
    while(lo <= hi){
        mid = (lo + hi) / 2LL ;
        if( (nx + p*mid - x) <= (ny + q*mid - y) ){
            ans = mid ;
            hi = mid - 1 ;
        }
        else lo = mid + 1;
    }
    return ans ;
}

int main(){
//    #ifndef ONLINE_JUDGE
//    freopen("input.txt" , "r" , stdin);
//    #endif
    int tst ,cas = 0 ;
    ll x , y , p , q , nx , ny , dx , dy ;
    sc1i( tst );
    while(tst--){
        sc4ll( x , y , p  , q) ;

        ll res = inf ;
        ll g = gcd( p , q );
        p /= g ;
        q /= g ;

        ll mx = (((y/q) + ((y % q) > 0 ? 1 :0 ))) ;

        nx = p * mx ;
        ny = q * mx ;

        ll k = bs(nx , ny , x , y , p , q ) ;

        nx += p*k ;
        ny += q*k ;

        if((nx - x) <= (ny - y)){
            dx = (nx - x) ;
            dy = (ny - y - dx) ;
            res = dx + dy ;
        }
        if(res == inf)pf("-1\n");
        else pf("%lld\n",res) ;
    }
    return 0;
}


#include <cassert>
#include <cctype>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <iostream>
#include <sstream>
#include <iomanip>
#include <string>
#include <vector>
#include <deque>
#include <list>
#include <set>
#include <map>
#include <bitset>
#include <stack>
#include <queue>
#include <algorithm>
#include <functional>
#include <iterator>
#include <numeric>
#include <utility>
#include <fstream>
#include <climits>
#include <complex>
#include <new>
#include <memory>
#include <time.h>
//#include <bits/stdc++.h>
using namespace std;

#define pf printf
#define sc scanf
#define ll long long int
#define ull unsigned ll

#define sc1i(a) sc("%d",&a)
#define sc2i(a,b) sc("%d%d",&a,&b)
#define sc3i(a,b,c) sc("%d%d%d",&a,&b,&c)
#define sc4i(a,b,c,d) sc("%d%d%d%d",&a,&b,&c,&d)

#define sc1ll(a) sc("%lld",&a)
#define sc2ll(a,b) sc("%lld%lld",&a,&b)
#define sc3ll(a,b,c) sc("%lld%lld%lld",&a,&b,&c)
#define sc4ll(a,b,c,d) sc("%lld%lld%lld%lld",&a,&b,&c,&d)

#define pb push_back
#define pi acos(-1.0)
#define mem(a,x) memset(a,x,sizeof(a))
#define all(v) v.begin(),v.end()
#define SZ(a) (int)a.size()
#define MP make_pair
#define sqr(a) ((a)*(a))
#define ppcnt(a) __builtin_popcount(a)
#define cnttz(a) __builtin_ctz(a)
#define cntlz(a) __builtin_clz(a)

#define rep(i,k,n) for(__typeof(n)i = k ; i <= n; i++)
#define rrep(i,k,n) for(__typeof(k)i = k ; i >= n; i--)
#define show(a) cerr <<#a<< " = " << a << endl;

#define wait system("pause")
#define ___ ios_base::sync_with_stdio(false);cin.tie(NULL);

//int dx[]= {-1,-1,0,0,1,1};
//int dy[]= {-1,0,-1,1,0,1};
int dx[]= {0,0,1,-1};//4 side move
int dy[]= {-1,1,0,0};//4 side move
//int dx[]= {1,1,0,-1,-1,-1,0,1};//8 side move
//int dy[]= {0,1,1,1,0,-1,-1,-1};//8 side move
//int dx[]={1,1,2,2,-1,-1,-2,-2};//knight move
//int dy[]={2,-2,1,-1,2,-2,1,-1};//knight move

template<class T> T power(T N,T P){ return (P==0)?  1: N*power(N,P-1);}//N^P
template<class T> T gcd(T a,T b){if(b == 0)return a;return gcd(b,a%b);}//gcd(a,b)

struct debugger{
    template<typename T> debugger& operator , (const T& v){
        cerr<< v <<" ";
        return *this;
    }
}dbg;
#define deb(args...) {dbg,args; cerr<<endl;}

void make_unique(vector<ll> &a){ sort(a.begin(), a.end());
    a.erase(unique(a.begin(), a.end()), a.end()); }
//ll bigmod(ll B,ll P,ll M){ ll R=1; while(P>0)  {if(P%2==1){R=(R*B)%M;}P/=2;B=(B*B)%M;} return R;} /// (B^P)%M

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

//int on(int n,int pos){return n=n|(1<<pos);}
//void off(int &n,int pos){ n = n & ~(1<<pos);}
//bool check(int n,int pos){return (bool)(n&(1<<pos));}
//N & (N % 2 ? 0 : ~0) | ( ((N & 2)>>1) ^ (N & 1) )://XOR of 1-n numbers

const int N = 1e6 + 10;
const ll inf = 2e18 ;

ll bs(ll x , ll y , ll p , ll q){
    ll lo = 1 , hi = inf , ans = hi;
    while(lo <= hi){
        ll mid = (lo + hi) / 2LL ;
        if(p*mid >= x && q*mid >= y && (p*mid - x) <= (q*mid - y)){
            ans = mid ;
            hi = mid - 1 ;
        }
        else lo = mid + 1 ;
    }
    return ans ;
}

int main(){
    #ifndef ONLINE_JUDGE
    freopen("input.txt" , "r" , stdin);
    #endif
    int tst ,cas = 0 ;
    ll x , y , p , q , nx , ny , dx , dy;
    sc1i( tst );
    while(tst--){
        sc4ll( x , y , p  , q) ;

        ll g = gcd( p , q ) ;

        p /= g ;
        q /= g ;

        ll res = bs( x , y , p , q ) ;

        if(res == inf)pf("-1\n");
        else pf("%lld\n",res*q - y) ;
    }
    return 0;
}


#include <cassert>
#include <cctype>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <iostream>
#include <sstream>
#include <iomanip>
#include <string>
#include <vector>
#include <deque>
#include <list>
#include <set>
#include <map>
#include <bitset>
#include <stack>
#include <queue>
#include <algorithm>
#include <functional>
#include <iterator>
#include <numeric>
#include <utility>
#include <fstream>
#include <climits>
#include <complex>
#include <new>
#include <memory>
#include <time.h>
//#include <bits/stdc++.h>
using namespace std;

#define pf printf
#define sc scanf
#define ll long long int
#define ull unsigned ll

#define sc1i(a) sc("%d",&a)
#define sc2i(a,b) sc("%d%d",&a,&b)
#define sc3i(a,b,c) sc("%d%d%d",&a,&b,&c)
#define sc4i(a,b,c,d) sc("%d%d%d%d",&a,&b,&c,&d)

#define sc1ll(a) sc("%lld",&a)
#define sc2ll(a,b) sc("%lld%lld",&a,&b)
#define sc3ll(a,b,c) sc("%lld%lld%lld",&a,&b,&c)
#define sc4ll(a,b,c,d) sc("%lld%lld%lld%lld",&a,&b,&c,&d)

#define pb push_back
#define pi acos(-1.0)
#define mem(a,x) memset(a,x,sizeof(a))
#define all(v) v.begin(),v.end()
#define SZ(a) (int)a.size()
#define MP make_pair
#define sqr(a) ((a)*(a))
#define ppcnt(a) __builtin_popcount(a)
#define cnttz(a) __builtin_ctz(a)
#define cntlz(a) __builtin_clz(a)

#define rep(i,k,n) for(__typeof(n)i = k ; i <= n; i++)
#define rrep(i,k,n) for(__typeof(k)i = k ; i >= n; i--)
#define show(a) cerr <<#a<< " = " << a << endl;

#define wait system("pause")
#define ___ ios_base::sync_with_stdio(false);cin.tie(NULL);

//int dx[]= {-1,-1,0,0,1,1};
//int dy[]= {-1,0,-1,1,0,1};
int dx[]= {0,0,1,-1};//4 side move
int dy[]= {-1,1,0,0};//4 side move
//int dx[]= {1,1,0,-1,-1,-1,0,1};//8 side move
//int dy[]= {0,1,1,1,0,-1,-1,-1};//8 side move
//int dx[]={1,1,2,2,-1,-1,-2,-2};//knight move
//int dy[]={2,-2,1,-1,2,-2,1,-1};//knight move

template<class T> T power(T N,T P){ return (P==0)?  1: N*power(N,P-1);}//N^P
template<class T> T gcd(T a,T b){if(b == 0)return a;return gcd(b,a%b);}//gcd(a,b)

struct debugger{
    template<typename T> debugger& operator , (const T& v){
        cerr<< v <<" ";
        return *this;
    }
}dbg;
#define deb(args...) {dbg,args; cerr<<endl;}

void make_unique(vector<ll> &a){ sort(a.begin(), a.end());
    a.erase(unique(a.begin(), a.end()), a.end()); }
//ll bigmod(ll B,ll P,ll M){ ll R=1; while(P>0)  {if(P%2==1){R=(R*B)%M;}P/=2;B=(B*B)%M;} return R;} /// (B^P)%M

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

//int on(int n,int pos){return n=n|(1<<pos);}
//void off(int &n,int pos){ n = n & ~(1<<pos);}
//bool check(int n,int pos){return (bool)(n&(1<<pos));}
//N & (N % 2 ? 0 : ~0) | ( ((N & 2)>>1) ^ (N & 1) )://XOR of 1-n numbers

const int N = 1e6 + 10;
const ll inf = 5e25 ;

ll bs(ll nx , ll ny , ll x , ll y , ll p , ll q){
    ll lo = 0LL , hi = (ll)2e10 , mid , ans = 2e10;
    while(lo <= hi){
        mid = (lo + hi) / 2LL ;
        if( (nx + p*mid - x) <= (ny + q*mid - y) ){
            ans = mid ;
            hi = mid - 1 ;
        }
        else lo = mid + 1;
    }
    return ans ;
}

int main(){
//    #ifndef ONLINE_JUDGE
//    freopen("input.txt" , "r" , stdin);
//    #endif
    int tst ,cas = 0 ;
    ll x , y , p , q , nx , ny , dx , dy ;
    sc1i( tst );
    while(tst--){
        sc4ll( x , y , p  , q) ;

        ll res = inf ;
        ll g = gcd( p , q );
        p /= g ;
        q /= g ;

        ll mx = (((y/q) + ((y % q) > 0 ? 1 :0 ))) ;

        nx = p * mx ;
        ny = q * mx ;

        ll k = bs(nx , ny , x , y , p , q ) ;

        nx += p*k ;
        ny += q*k ;

        if((nx - x) <= (ny - y)){
            dx = (nx - x) ;
            dy = (ny - y - dx) ;
            res = dx + dy ;
        }

        if(res == inf)pf("-1\n");
        else pf("%lld\n",res) ;
    }
    return 0;
}


#include <cassert>
#include <cctype>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <iostream>
#include <sstream>
#include <iomanip>
#include <string>
#include <vector>
#include <deque>
#include <list>
#include <set>
#include <map>
#include <bitset>
#include <stack>
#include <queue>
#include <algorithm>
#include <functional>
#include <iterator>
#include <numeric>
#include <utility>
#include <fstream>
#include <climits>
#include <complex>
#include <new>
#include <memory>
#include <time.h>
//#include <bits/stdc++.h>
using namespace std;

#define pf printf
#define sc scanf
#define ll long long int
#define ull unsigned ll

#define sc1i(a) sc("%d",&a)
#define sc2i(a,b) sc("%d%d",&a,&b)
#define sc3i(a,b,c) sc("%d%d%d",&a,&b,&c)
#define sc4i(a,b,c,d) sc("%d%d%d%d",&a,&b,&c,&d)

#define sc1ll(a) sc("%lld",&a)
#define sc2ll(a,b) sc("%lld%lld",&a,&b)
#define sc3ll(a,b,c) sc("%lld%lld%lld",&a,&b,&c)
#define sc4ll(a,b,c,d) sc("%lld%lld%lld%lld",&a,&b,&c,&d)

#define pb push_back
#define pi acos(-1.0)
#define mem(a,x) memset(a,x,sizeof(a))
#define all(v) v.begin(),v.end()
#define SZ(a) (int)a.size()
#define MP make_pair
#define sqr(a) ((a)*(a))
#define ppcnt(a) __builtin_popcount(a)
#define cnttz(a) __builtin_ctz(a)
#define cntlz(a) __builtin_clz(a)

#define rep(i,k,n) for(__typeof(n)i = k ; i <= n; i++)
#define rrep(i,k,n) for(__typeof(k)i = k ; i >= n; i--)
#define show(a) cerr <<#a<< " = " << a << endl;

#define wait system("pause")
#define ___ ios_base::sync_with_stdio(false);cin.tie(NULL);

//int dx[]= {-1,-1,0,0,1,1};
//int dy[]= {-1,0,-1,1,0,1};
int dx[]= {0,0,1,-1};//4 side move
int dy[]= {-1,1,0,0};//4 side move
//int dx[]= {1,1,0,-1,-1,-1,0,1};//8 side move
//int dy[]= {0,1,1,1,0,-1,-1,-1};//8 side move
//int dx[]={1,1,2,2,-1,-1,-2,-2};//knight move
//int dy[]={2,-2,1,-1,2,-2,1,-1};//knight move

template<class T> T power(T N,T P){ return (P==0)?  1: N*power(N,P-1);}//N^P
template<class T> T gcd(T a,T b){if(b == 0)return a;return gcd(b,a%b);}//gcd(a,b)

struct debugger{
    template<typename T> debugger& operator , (const T& v){
        cerr<< v <<" ";
        return *this;
    }
}dbg;
#define deb(args...) {dbg,args; cerr<<endl;}

void make_unique(vector<ll> &a){ sort(a.begin(), a.end());
    a.erase(unique(a.begin(), a.end()), a.end()); }
//ll bigmod(ll B,ll P,ll M){ ll R=1; while(P>0)  {if(P%2==1){R=(R*B)%M;}P/=2;B=(B*B)%M;} return R;} /// (B^P)%M

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

//int on(int n,int pos){return n=n|(1<<pos);}
//void off(int &n,int pos){ n = n & ~(1<<pos);}
//bool check(int n,int pos){return (bool)(n&(1<<pos));}
//N & (N % 2 ? 0 : ~0) | ( ((N & 2)>>1) ^ (N & 1) )://XOR of 1-n numbers

const int N = 1e6 + 10;
const ll inf = 2e18 ;

ll bs(ll x , ll y , ll p , ll q){
    ll lo = 1 , hi = inf , ans = hi;
    while(lo <= hi){
        ll mid = (lo + hi) / 2LL ;
        if(p*mid >= x && q*mid >= y && (p*mid - x) <= (q*mid - y)){
            ans = mid ;
            hi = mid - 1 ;
        }
        else lo = mid + 1 ;
    }
    return ans ;
}

int main(){
    #ifndef ONLINE_JUDGE
    freopen("input.txt" , "r" , stdin);
    #endif
    int tst ,cas = 0 ;
    ll x , y , p , q , nx , ny , dx , dy;
    sc1i( tst );
    while(tst--){
        sc4ll( x , y , p  , q) ;

        ll g = gcd( p , q ) ;

        p /= g ;
        q /= g ;
        if(p == 0){
            pf("%d\n",(x==0) ? 0 : -1);
        }
        else if(p == q){
            pf("%d\n",(x == y) ? 0 : -1);
        }
        else{
            ll res = bs( x , y , p , q ) ;
            pf("%lld\n",res * q - y);
        }

    }
    return 0;
}


#include <stdio.h>
#include <bits/stdc++.h>
#define read(); freopen("input.txt","r",stdin);
using namespace std;
typedef long long ll;
int t;
int gcd(int n,int m){
	if(n%m==0) return m;
	return gcd(m,n%m);
}
int main()
{
	cin>>t;
	while(t--){
		ll x,y,p,q;
		cin>>x>>y>>p>>q;
		if(p && p/q==1){
			if(x/y==1)
				cout<<0<<endl;
			else cout<<-1<<endl;
			continue;
		}
		if(q==0){
			if(x==0)
				cout<<0<<endl;
			else cout<<-1<<endl;
			continue;
		}
		if(x==0){
			if(q==0)
				cout<<0<<endl;
			else cout<<-1<<endl;
			continue;
		}
		int k=gcd(p,q);
		p/=k;q/=k;
		ll ans=0;
		for(int i=max(y/q,x/p);1;i++){
			if(x<=p*i && y+(p*i-x)<=q*i){
				ans=p*i-x+ q*i-(p*i-x+y);
				cout<<ans<<endl;
				break;
			}
		}
	}

	return 0;
}


#include <stdio.h>
#include <bits/stdc++.h>
#define read(); freopen("input.txt","r",stdin);
using namespace std;
typedef long long ll;
int t;
int gcd(int n,int m){
	if(n%m==0) return m;
	return gcd(m,n%m);
}
int main()
{
	cin>>t;
	while(t--){
		ll x,y,p,q;
		cin>>x>>y>>p>>q;
		if(p/q==1){
			if(x/y==1)
				cout<<0<<endl;
			else cout<<-1<<endl;
			continue;
		}
		if(p==0){
			if(x==0)
				cout<<0<<endl;
			else cout<<-1<<endl;
			continue;
		}
		int k=gcd(p,q);
		p/=k;q/=k;
		ll ans=0;
		ll maxn= (y-x)/(q-p);
		maxn=max(maxn,y/q);
		maxn=max(maxn,x/p);
		for(ll i=maxn;1;i++){
			if(i==0)continue;
			if(x<=p*i && p*i-x+y<=q*i){
				ans=p*i-x+ q*i-(p*i-x+y);
			//	cout<<"ans: "<<i<<endl;;
				cout<<ans<<endl;
				break;
			}
		}
	}

	return 0;
}


#include <stdio.h>
#include <bits/stdc++.h>
#define read(); freopen("input.txt","r",stdin);
using namespace std;
typedef long long ll;
int t;
int gcd(int n,int m){
	if(n%m==0) return m;
	return gcd(m,n%m);
}
int main()
{
	cin>>t;
	while(t--){
		ll x,y,p,q;
		cin>>x>>y>>p>>q;
		ll k=gcd(x,y);
	//	x/=k;y/=k;
		k=gcd(p,q);
		p/=k;q/=k;
		if(p && p/q==1){
			if(x/y==1)
				cout<<0<<endl;
			else cout<<-1<<endl;
			continue;
		}
		if(q==0){
			if(x==0)
				cout<<0<<endl;
			else cout<<-1<<endl;
			continue;
		}
		ll ans=0;
		for(int i=y/q;1;i++){
			if(x<=p*i && y+(p*i-x)<=q*i){
				ans=p*i-x+ q*i-(p*i-x+y);
				cout<<ans<<endl;
				break;
			}
		}
	}

	return 0;
}


#include <stdio.h>
#include <bits/stdc++.h>
#define read(); freopen("input.txt","r",stdin);
using namespace std;
typedef long long ll;
int t;
int gcd(int n,int m){
	if(n%m==0) return m;
	return gcd(m,n%m);
}
int main()
{
	cin>>t;
	while(t--){
		ll x,y,p,q;
		cin>>x>>y>>p>>q;
		if(p && p/q==1){
			if(x/y==1)
				cout<<0<<endl;
			else cout<<-1<<endl;
			continue;
		}
		if(q==0){
			if(x==0)
				cout<<0<<endl;
			else cout<<-1<<endl;
			continue;
		}
		int k=gcd(p,q);
		p/=k;q/=k;
		ll ans=0;
		for(int i=y/q;1;i++){
			if(x<=p*i && y+(p*i-x)<=q*i){
				ans=p*i-x+ q*i-(p*i-x+y);
				cout<<ans<<endl;
				break;
			}
		}
	}

	return 0;
}


#include <stdio.h>
#include <bits/stdc++.h>
#define read(); freopen("input.txt","r",stdin);
using namespace std;
typedef long long ll;
int t;
int gcd(int n,int m){
	if(n%m==0) return m;
	return gcd(m,n%m);
}
int main()
{
	cin>>t;
	while(t--){
		ll x,y,p,q;
		cin>>x>>y>>p>>q;
		if(p/q==1){
			if(x/y==1)
				cout<<0<<endl;
			else cout<<-1<<endl;
			continue;
		}
		if(p==0){
			if(x==0)
				cout<<0<<endl;
			else cout<<-1<<endl;
			continue;
		}
		int k=gcd(p,q);
		p/=k;q/=k;
		ll ans=0;
		ll maxn= (y-x)/(q-p)+((y-x)%(q-p)?1:0);
		maxn=max(maxn,y/q+(y%q?1:0));
		maxn=max(maxn,x/p+(x%p?1:0));
		ans=q*maxn-y;
		cout<<ans<<endl;
	}
	return 0;
}


#include <stdio.h>
#include <bits/stdc++.h>
#define read(); freopen("input.txt","r",stdin);
using namespace std;
typedef long long ll;
int t;
int gcd(int n,int m){
	if(n%m==0) return m;
	return gcd(m,n%m);
}
int main()
{
	cin>>t;
	while(t--){
		ll x,y,p,q;
		cin>>x>>y>>p>>q;
		if(p/q==1){
			if(x/y==1)
				cout<<0<<endl;
			else cout<<-1<<endl;
			continue;
		}
		if(p==0){
			if(x==0)
				cout<<0<<endl;
			else cout<<-1<<endl;
			continue;
		}
		int k=gcd(p,q);
		p/=k;q/=k;
		ll ans=0;
		for(int i=max(y/q,x/p);1;i++){
			if(x<=p*i && y+(p*i-x)<=q*i){
				ans=p*i-x+ q*i-(p*i-x+y);
				cout<<ans<<endl;
				break;
			}
		}
	}

	return 0;
}


#include <stdio.h>
#include <bits/stdc++.h>
#define read(); freopen("input.txt","r",stdin);
using namespace std;
typedef long long ll;
int t;
int gcd(int n,int m){
	if(n%m==0) return m;
	return gcd(m,n%m);
}
int main()
{
	cin>>t;
	while(t--){
		ll x,y,p,q;
		cin>>x>>y>>p>>q;
		if(p/q==1){
			if(x/y==1)
				cout<<0<<endl;
			else cout<<-1<<endl;
			continue;
		}
		if(p==0){
			if(x==0)
				cout<<0<<endl;
			else cout<<-1<<endl;
			continue;
		}
		int k=gcd(p,q);
		p/=k;q/=k;
		ll ans=0;
		for(ll i=(y-x)/(q-p);1;i++){
			if(i==0)continue;
			if(x<=p*i && (y-x)/(q-p)<=i){
				ans=p*i-x+ q*i-(p*i-x+y);
			//	cout<<"ans: "<<i<<endl;;
				cout<<ans<<endl;
				break;
			}
		}
	}

	return 0;
}
/*
1
1 999999999 999999999 1000000000

*/


#include <stdio.h>
#include <bits/stdc++.h>
#define read(); freopen("input.txt","r",stdin);
using namespace std;
typedef long long ll;
int t;
int gcd(int n,int m){
	if(n%m==0) return m;
	return gcd(m,n%m);
}
int main()
{
	cin>>t;
	while(t--){
		ll x,y,p,q;
		cin>>x>>y>>p>>q;
		if(p && p/q==1){
			if(x/y==1)
				cout<<0<<endl;
			else cout<<-1<<endl;
			continue;
		}
		if(q==0){
			if(x==0)
				cout<<0<<endl;
			else cout<<-1<<endl;
			continue;
		}
		int k=gcd(p,q);
		p/=k;q/=k;
		ll ans=0;
		for(int i=max(y/q,x/p);1;i++){
			if(x<=p*i && y+(p*i-x)<=q*i){
				ans=p*i-x+ q*i-(p*i-x+y);
				cout<<ans<<endl;
				break;
			}
		}
	}

	return 0;
}


#include <stdio.h>
#include <bits/stdc++.h>
#define read(); freopen("input.txt","r",stdin);
using namespace std;
typedef long long ll;
int t;
int gcd(int n,int m){
	if(n%m==0) return m;
	return gcd(m,n%m);
}
int main()
{
	cin>>t;
	while(t--){
		ll x,y,p,q;
		cin>>x>>y>>p>>q;
		if(p && p/q==1){
			if(x/y==1)
				cout<<0<<endl;
			else cout<<-1<<endl;
			continue;
		}
		if(q==0){
			if(x==0)
				cout<<0<<endl;
			else cout<<-1<<endl;
			continue;
		}
		k=gcd(p,q);
		p/=k;q/=k;
		ll ans=0;
		for(int i=y/q;1;i++){
			if(x<=p*i && y+(p*i-x)<=q*i){
				ans=p*i-x+ q*i-(p*i-x+y);
				cout<<ans<<endl;
				break;
			}
		}
	}

	return 0;
}


#include <stdio.h>
#include <bits/stdc++.h>
#define read(); freopen("input.txt","r",stdin);
using namespace std;
typedef long long ll;
int t;
int gcd(int n,int m){
	if(n%m==0) return m;
	return gcd(m,n%m);
}
int main()
{
	cin>>t;
	while(t--){
		ll x,y,p,q;
		cin>>x>>y>>p>>q;
		if(p && p/q==1){
			if(x/y==1)
				cout<<0<<endl;
			else cout<<-1<<endl;
			continue;
		}
		if(p==0){
			if(x==0)
				cout<<0<<endl;
			else cout<<-1<<endl;
			continue;
		}
		if(x==0){
			if(p==0)
				cout<<0<<endl;
			else cout<<-1<<endl;
			continue;
		}
		int k=gcd(p,q);
		p/=k;q/=k;
		ll ans=0;
		for(int i=max(y/q,x/p);1;i++){
			if(x<=p*i && y+(p*i-x)<=q*i){
				ans=p*i-x+ q*i-(p*i-x+y);
				cout<<ans<<endl;
				break;
			}
		}
	}

	return 0;
}


#include <stdio.h>
#include <bits/stdc++.h>
#define read(); freopen("input.txt","r",stdin);
using namespace std;
typedef long long ll;
int t;
int gcd(int n,int m){
	if(n%m==0) return m;
	return gcd(m,n%m);
}
int main()
{
	cin>>t;
	while(t--){
		ll x,y,p,q;
		cin>>x>>y>>p>>q;
		if(p/q==1){
			if(x/y==1)
				cout<<0<<endl;
			else cout<<-1<<endl;
			continue;
		}
		if(p==0){
			if(x==0)
				cout<<0<<endl;
			else cout<<-1<<endl;
			continue;
		}
		int k=gcd(p,q);
		p/=k;q/=k;
		ll ans=0;
		for(ll i=(y-x)/(q-p);1;i++){
			if(i==0)continue;
			if(x<=p*i && p*i-x+y<=q*i){
				ans=p*i-x+ q*i-(p*i-x+y);
			//	cout<<"ans: "<<i<<endl;;
				cout<<ans<<endl;
				break;
			}
		}
	}

	return 0;
}


#include <bits/stdc++.h>
using namespace std;
#define ll long long
int main(){
	int t;
	cin >> t;
	while(t--){
		ll x, y, q, p;
		cin >> x >> y >> p >> q;
		if(q == p){
			printf("%d\n",(x==y)?0:-1);
			continue;
		}
		if(x == 0){
			printf("%d\n",(x==0)?0:-1);
			continue;
		}
		ll GCD = __gcd(q,p);
		q/=GCD; p/=GCD;
		ll n = max(ceil(x*1.0/p),ceil((y-x)*1.0/(q-p)));
		printf("%lld\n",n*q-y);
	}
	return 0;
}


#include <bits/stdc++.h>
using namespace std;
#define ll long long
ll x, y, q, p;
int main(){
	int t;
	cin >> t;
	while(t--){
		cin >> x >> y >> p >> q;
		if(q == p){
			printf("%d\n",(x==y)?0:-1);
			continue;
		}
		if(p == 0){
			printf("%d\n",(x==0)?0:-1);
			continue;
		}
		ll GCD = __gcd(q,p);
		q/=GCD; p/=GCD;
		ll n = max(ceil(x*1.0/p),ceil((y-x)*1.0/(q-p)));
		printf("%lld\n",n*q-y);
	}
	return 0;
}


#include <bits/stdc++.h>
#define ll long long
using namespace std;
ll t, x, y, p, q;
int main(){
	cin >> t;
	while(t--){
		cin >> x >> y >> p >> q;
		if(p == q){
			printf("%d\n",(x==y)?0:-1);
			continue;
		}
		if(p == 0){
			printf("%d\n",(x==0)?0:-1);
			continue;
		}
		ll GCD = __gcd(p,q);
		p/=GCD; q/=GCD;
		ll n = max(ceil(x*1.0/p),ceil((y-x)*1.0)/(q-p));
		printf("%lld\n",n*q-y);
	}
	return 0;
}


#include <bits/stdc++.h>
#define ll long long
using namespace std;
ll t, x, y, p, q;
int main(){
	cin >> t;
	while(t--){
		cin >> x >> y >> p >> q;
		if(p == q){
			printf("%d\n",(x==y)?0:-1);
			continue;
		}
		f(p == 0){
			printf("%d\n",(x==0)?0:-1);
			continue;
		}
		ll GCD = __gcd(p,q);
		p/=GCD; q/=GCD;
		ll n = max(ceil(x*1.0/p),ceil((y-x)*1.0)/(q-p));
		printf("%lld\n",n*q-y);
	}
	return 0;
}


#include<bits/stdc++.h>
using namespace std;
long long x, y, p, q;
int main()
{
    int T;
    scanf("%d",&T);
    while(T--)
    {
        cin>>x>>y>>p>>q;
        if(p==q){
            printf("%d\n", x==y?0:-1);  continue;
        }
        if(p==0){
            printf("%d\n", x==0?0:-1);  continue;
        }
        long long GCD = __gcd(p, q);
        p /= GCD,   q /= GCD;
        long long n = max(ceil(x*1.0/p), ceil((y-x)*1.0/(q-p)));
        printf("%I64d\n", n*q-y);
    }
}


#include <bits/stdc++.h>
#define ll long long
using namespace std;
int main(){
	ll t, x, y, p, q;
	cin >> t;
	while(t--){
		cin >> x >> y >> p >> q;
		if(p == q){
			printf("%d\n",(x==y)?0:-1);
		}else if(!p){
			printf("%d\n",(x==0)?0:-1);
		}else{
			ll GCD = __gcd(p,q);
			p/=GCD; q/=GCD;
			ll n = max(ceil(x*1.0/p),ceil((y-x)*1.0)/(q-p));
			printf("%lld\n",n*q-y);
		}
	}
	return 0;
}


#include <bits/stdc++.h>
#define ll long long
using namespace std;
ll t, x, y, p, q;
int main(){
	cin >> t;
	while(t--){
		cin >> x >> y >> p >> q;
		if(p == q){
			printf("%d\n",(x==y)?0:-1);
		}else if(p == 0){
			printf("%d\n",(x==0)?0:-1);
		}else{
			ll GCD = __gcd(p,q);
			p/=GCD; q/=GCD;
			ll n = max(ceil(x*1.0/p),ceil((y-x)*1.0)/(q-p));
			printf("%lld\n",n*q-y);
		}
	}
	return 0;
}


#include <bits/stdc++.h>
#define ll long long
using namespace std;
ll t, x, y, p, q;
int main(){
	cin >> t;
	while(t--){
		cin >> x >> y >> p >> q;
		if(p == q){
			printf("%d\n",(x==y)?0:-1);
			continue;
		}
		if(p == 0){
			printf("%d\n",(x==0)?0:-1);
			continue;
		}
		ll GCD = __gcd(p,q);
		p/=GCD; q/=GCD;
		ll n = max(ceil(x*1.0/p),ceil((y-x)*1.0)/(q-p));
		cout<<n*q-y<<endl;
	}
	return 0;
}


#include <bits/stdc++.h>
#define ll long long
using namespace std;
int main(){
	ll t, x, y, a, b;
	cin >> t;
	while(t--){
		ll ans = 0;
		cin >> x >> y >> a >> b;
		if(a == b && (x < y)){
			ans = -1;
		}else{
			if(x*b < a*y){
				ll temp = b-y%b;
				y+=temp;x+=temp;
				ans+=temp;
				while(x*b < a*y){
					y+=b;x+=b;
					ans+=b;
				}
			}else if(x*b == a*y){
				ans = 0;
			}else if(x*b > a*y){
				ll temp = b-y%b;
				y+=temp;ans+=temp;
				while(x*b > a*y){
					y+=b;
					ans+=b;
				}
			}
		}
		cout << ans << endl;
	}
	return 0;
}


#include <bits/stdc++.h>
#define ll long long
using namespace std;
int main(){
	ll t, x, y, p, q;
	cin >> t;
	while(t--){
		cin >> x >> y >> p >> q;
		if(p == q){
			printf("%d\n",(x==y)?0:-1);
		}else if(!p){
			printf("%d\n",(x==0)?0:-1);
		}else{
			ll n = max(ceil(x*1.0/p),ceil((y-x)*1.0)/(q-p));
			printf("%lld\n",n*q-y);
		}
	}
	return 0;
}


#include<bits/stdc++.h>
#define ll unsigned long long int
using namespace std;
int main()
{ios::sync_with_stdio(0);
 ll t,p,q,x,y,i,j,cr,ans,num,dif,lo,mid,high;

 cin>>t;
    while(t--)
       {ans=dif=999999999999999999;
        cin>>x>>y>>p>>q;

        if((p==q &&x!=y)  || (p==0 &&x!=0) ||q==0 )
           {
           cout<<-1<<endl;
           continue;
           }
           if(p==0 &&x==0)
           {cout<<0<<endl;continue;}
          if(y%q==0)
             cr=y/q;
             else
             cr=(y/q)+1;
             lo=cr;
             high=9999999999;

                  while(lo<high-1)
                  {
                   mid=(lo+high)/2;
                   //cout<<"mid "<<mid <<endl;
                      if((mid*q)>= y &&(mid*p)>=x)
                      {//cout<<" yoolo"<<endl;
                    //  cout<<(mid*q)-y<<endl;
                     // cout<<((mid*p)-x)<<endl;
                          if((mid*q)-y -((mid*p)-x)<=dif && (mid*p)-x <=(mid*q)-y)
                          {//cout<<"yes"<<endl;
                              dif=(mid*q)-y -((mid*p)-x);

                              ans=min(ans,(mid*q)-y);

                              high=mid;
                          }
                          else
                            lo=mid;
                      }
                      else
                      {
                          lo=mid;
                      }
                  }
                 // cout<<" lo is "<<lo <<"   h igh os "<<high<<endl;
                  if((high*q)-y -((high*p)-x)<=dif )
                          {
                              dif=(mid*q)-y -((mid*p)-x);
                              ans=min(ans,(high*q)-y);
                             //cout<<"an 5 :"<<ans<<endl;
                          }
                 if((lo*q)-y -((lo*p)-x)<=dif && (lo*q)- y >=(lo*p)-x && (lo*q)- y >=0 &&(lo*p)-x >=0)
                          {

                              ans=min(ans,(lo*q)-y);
                        //      cout<<"an 577 :"<<ans<<endl;
                          }

                  cout<<ans<<endl;
       }

return 0;
}


#include<bits/stdc++.h>
#define ll long long int
using namespace std;
int main()
{ios::sync_with_stdio(0);
 ll t,p,q,x,y,i,j,cr,ans,num,dif,lo,mid,high;

 cin>>t;
    while(t--)
       {ans=dif=999999999999;
        cin>>x>>y>>p>>q;

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

          if(y%q==0)
             cr=y/q;
             else
             cr=(y/q)+1;
             lo=cr;
             high=999999999;

                  while(lo<high-1)
                  {
                   mid=(lo+high)/2;
                   //cout<<"mid "<<mid <<endl;
                      if((mid*q)>= y &&(mid*p)>=x)
                      {
                          if((mid*q)-y -((mid*p)-x)<=dif && (mid*p)-x <=(mid*q)-y)
                          {//cout<<"yes"<<endl;
                              dif=(mid*q)-y -((mid*p)-x);

                              ans=min(ans,(mid*q)-y);

                              high=mid;
                          }
                          else
                            lo=mid;
                      }
                      else
                      {
                          lo=mid;
                      }
                  }
                 // cout<<" lo is "<<lo <<"   h igh os "<<high<<endl;
                  if((high*q)-y -((high*p)-x)<=dif )
                          {
                              dif=(mid*q)-y -((mid*p)-x);
                              ans=min(ans,(high*q)-y);
                           //   cout<<"an 5 :"<<ans<<endl;
                          }
                 if((lo*q)-y -((lo*p)-x)<=dif && (lo*q)- y >=(lo*p)-x && (lo*q)- y >=0 &&(lo*p)-x >=0)
                          {

                              ans=min(ans,(lo*q)-y);
                            //  cout<<"an 577 :"<<ans<<endl;
                          }

                  cout<<ans<<endl;
       }

return 0;
}


#include<bits/stdc++.h>
#define ll long long int
using namespace std;
int main()
{ios::sync_with_stdio(0);
 ll t,p,q,x,y,i,j,cr,ans,num;
 cin>>t;
    while(t--)
       {
        cin>>x>>y>>p>>q;

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

          if(y%q==0)
             cr=y/q;
             else
             cr=(y/q)+1;
                for(;;cr++)
                  {
                  num=cr*p;
                   if(num<x)
                     {

                     }
                     else
                     {
                       if(num-x <= (cr*q)-y)
                          {ans=(cr*q)-y;
                            break;
                          }
                     }
                  }
                  cout<<ans<<endl;
       }

return 0;
}


#include<bits/stdc++.h>
#define ll long long int
using namespace std;
int main()
{ios::sync_with_stdio(0);
 ll t,p,q,x,y,i,j,cr,ans,num,dif,lo,mid,high;

 cin>>t;
    while(t--)
       {ans=dif=999999999999;
        cin>>x>>y>>p>>q;

        if((p==q &&x!=y)  || (p==0 &&x!=0) ||q==0 )
           {
           cout<<-1<<endl;
           continue;
           }
           if(p==0 &&x==0)
           {cout<<0<<endl;continue;}
          if(y%q==0)
             cr=y/q;
             else
             cr=(y/q)+1;
             lo=cr;
             high=999999999;

                  while(lo<high-1)
                  {
                   mid=(lo+high)/2;
                   //cout<<"mid "<<mid <<endl;
                      if((mid*q)>= y &&(mid*p)>=x)
                      {
                          if((mid*q)-y -((mid*p)-x)<=dif && (mid*p)-x <=(mid*q)-y)
                          {//cout<<"yes"<<endl;
                              dif=(mid*q)-y -((mid*p)-x);

                              ans=min(ans,(mid*q)-y);

                              high=mid;
                          }
                          else
                            lo=mid;
                      }
                      else
                      {
                          lo=mid;
                      }
                  }
                 // cout<<" lo is "<<lo <<"   h igh os "<<high<<endl;
                  if((high*q)-y -((high*p)-x)<=dif )
                          {
                              dif=(mid*q)-y -((mid*p)-x);
                              ans=min(ans,(high*q)-y);
                           //   cout<<"an 5 :"<<ans<<endl;
                          }
                 if((lo*q)-y -((lo*p)-x)<=dif && (lo*q)- y >=(lo*p)-x && (lo*q)- y >=0 &&(lo*p)-x >=0)
                          {

                              ans=min(ans,(lo*q)-y);
                            //  cout<<"an 577 :"<<ans<<endl;
                          }

                  cout<<ans<<endl;
       }

return 0;
}


#include<bits/stdc++.h>
#define ll long long int
using namespace std;
int main()
{ios::sync_with_stdio(0);
 ll t,p,q,x,y,i,j,cr,ans,num,dif,lo,mid,high;

 cin>>t;
    while(t--)
       {ans=dif=999999999999999999;
        cin>>x>>y>>p>>q;

        if((p==q &&x!=y)  || (p==0 &&x!=0) ||q==0 )
           {
           cout<<-1<<endl;
           continue;
           }
           if(p==0 &&x==0)
           {cout<<0<<endl;continue;}
          if(y%q==0)
             cr=y/q;
             else
             cr=(y/q)+1;
             lo=cr;
             high=999999999;

                  while(lo<high-1)
                  {
                   mid=(lo+high)/2;
                   //cout<<"mid "<<mid <<endl;
                      if((mid*q)>= y &&(mid*p)>=x)
                      {//cout<<" yoolo"<<endl;
                    //  cout<<(mid*q)-y<<endl;
                     // cout<<((mid*p)-x)<<endl;
                          if((mid*q)-y -((mid*p)-x)<=dif && (mid*p)-x <=(mid*q)-y)
                          {//cout<<"yes"<<endl;
                              dif=(mid*q)-y -((mid*p)-x);

                              ans=min(ans,(mid*q)-y);

                              high=mid;
                          }
                          else
                            lo=mid;
                      }
                      else
                      {
                          lo=mid;
                      }
                  }
                 // cout<<" lo is "<<lo <<"   h igh os "<<high<<endl;
                  if((high*q)-y -((high*p)-x)<=dif )
                          {
                              dif=(mid*q)-y -((mid*p)-x);
                              ans=min(ans,(high*q)-y);
                             //cout<<"an 5 :"<<ans<<endl;
                          }
                 if((lo*q)-y -((lo*p)-x)<=dif && (lo*q)- y >=(lo*p)-x && (lo*q)- y >=0 &&(lo*p)-x >=0)
                          {

                              ans=min(ans,(lo*q)-y);
                        //      cout<<"an 577 :"<<ans<<endl;
                          }

                  cout<<ans<<endl;
       }

return 0;
}


#include<bits/stdc++.h>
#define ll long long int
using namespace std;
int main()
{ios::sync_with_stdio(0);
ll x,y,p,q,i,j,t,l,r,m;
  cin>>t;
   while(t--)
      {
        cin>>x>>y>>p>>q;
          if((p==0 &&x>0) || (p==q && x!=y ))
            {cout<<-1<<endl;continue;}
           if(x*q==y*p)
             {cout<<0<<endl;continue;}
             l=1; r=1000000007;
              while(l<r)
                {
                 m=(l+r)/2;
                  if(p*m>=x && q*m>=y+(p*m-x))
                      r=m;
                      else
                      l=m+1;

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

return 0;
}


#include<bits/stdc++.h>
#define ll long long int
using namespace std;
int main()
{ios::sync_with_stdio(0);
ll x,y,p,q,i,j,t,l,r,m;
  cin>>t;
   while(t--)
      {
        cin>>x>>y>>p>>q;
          if((p==0 &&x>0) || (p==q && x!=y ))
            {cout<<-1<<endl;continue;}
           if(x*q==y*p)
             {cout<<0<<endl;continue;}
             l=1; r=10000007;
              while(l<r)
                {
                 m=(l+r)/2;
                  if(p*m>=x && q*m>=y+(p*m-x))
                      r=m;
                      else
                      l=m+1;

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

return 0;
}


#include<bits/stdc++.h>
#define ll long long int
using namespace std;
int main()
{ios::sync_with_stdio(0);
 ll t,p,q,x,y,i,j,cr,ans,num,dif,lo,mid,high;

 cin>>t;
    while(t--)
       {ans=dif=999999999999999999;
        cin>>x>>y>>p>>q;

        if((p==q &&x!=y)  || (p==0 &&x!=0) ||q==0 )
           {
           cout<<-1<<endl;
           continue;
           }
           if(p==0 &&x==0)
           {cout<<0<<endl;continue;}
          if(y%q==0)
             cr=y/q;
             else
             cr=(y/q)+1;
             lo=cr;
             high=999999999999999999;

                  while(lo<high-1)
                  {
                   mid=(lo+high)/2;
                   //cout<<"mid "<<mid <<endl;
                      if((mid*q)>= y &&(mid*p)>=x)
                      {//cout<<" yoolo"<<endl;
                    //  cout<<(mid*q)-y<<endl;
                     // cout<<((mid*p)-x)<<endl;
                          if((mid*q)-y -((mid*p)-x)<=dif && (mid*p)-x <=(mid*q)-y)
                          {//cout<<"yes"<<endl;
                              dif=(mid*q)-y -((mid*p)-x);

                              ans=min(ans,(mid*q)-y);

                              high=mid;
                          }
                          else
                            lo=mid;
                      }
                      else
                      {
                          lo=mid;
                      }
                  }
                 // cout<<" lo is "<<lo <<"   h igh os "<<high<<endl;
                  if((high*q)-y -((high*p)-x)<=dif )
                          {
                              dif=(mid*q)-y -((mid*p)-x);
                              ans=min(ans,(high*q)-y);
                             //cout<<"an 5 :"<<ans<<endl;
                          }
                 if((lo*q)-y -((lo*p)-x)<=dif && (lo*q)- y >=(lo*p)-x && (lo*q)- y >=0 &&(lo*p)-x >=0)
                          {

                              ans=min(ans,(lo*q)-y);
                        //      cout<<"an 577 :"<<ans<<endl;
                          }

                  cout<<ans<<endl;
       }

return 0;
}


#include<bits/stdc++.h>
#define ll long long int
using namespace std;
int main()
{ios::sync_with_stdio(0);
 ll t,p,q,x,y,i,j,cr,ans,num,dif,lo,mid,high;

 cin>>t;
    while(t--)
       {ans=dif=999999999999999999;
        cin>>x>>y>>p>>q;

        if((p==q &&x!=y)  || (p==0 &&x!=0) ||q==0 )
           {
           cout<<-1<<endl;
           continue;
           }
           if(p==0 &&x==0)
           {cout<<0<<endl;continue;}
          if(y%q==0)
             cr=y/q;
             else
             cr=(y/q)+1;
             lo=cr;
             high=9999999999999999;

                  while(lo<high-1)
                  {
                   mid=(lo+high)/2;
                   //cout<<"mid "<<mid <<endl;
                      if((mid*q)>= y &&(mid*p)>=x)
                      {//cout<<" yoolo"<<endl;
                    //  cout<<(mid*q)-y<<endl;
                     // cout<<((mid*p)-x)<<endl;
                          if((mid*q)-y -((mid*p)-x)<=dif && (mid*p)-x <=(mid*q)-y)
                          {//cout<<"yes"<<endl;
                              dif=(mid*q)-y -((mid*p)-x);

                              ans=min(ans,(mid*q)-y);

                              high=mid;
                          }
                          else
                            lo=mid;
                      }
                      else
                      {
                          lo=mid;
                      }
                  }
                 // cout<<" lo is "<<lo <<"   h igh os "<<high<<endl;
                  if((high*q)-y -((high*p)-x)<=dif )
                          {
                              dif=(mid*q)-y -((mid*p)-x);
                              ans=min(ans,(high*q)-y);
                             //cout<<"an 5 :"<<ans<<endl;
                          }
                 if((lo*q)-y -((lo*p)-x)<=dif && (lo*q)- y >=(lo*p)-x && (lo*q)- y >=0 &&(lo*p)-x >=0)
                          {

                              ans=min(ans,(lo*q)-y);
                        //      cout<<"an 577 :"<<ans<<endl;
                          }

                  cout<<ans<<endl;
       }

return 0;
}


#include<bits/stdc++.h>
#define ll long long int
using namespace std;
int main()
{ios::sync_with_stdio(0);
 ll t,p,q,x,y,i,j,cr,ans,num,dif,lo,mid,high;

 cin>>t;
    while(t--)
       {ans=dif=999999999999;
        cin>>x>>y>>p>>q;

        if(p==q)
           {
           cout<<-1<<endl;
           continue;
           }

          if(y%q==0)
             cr=y/q;
             else
             cr=(y/q)+1;
             lo=cr;
             high=999999999;

                  while(lo<high-1)
                  {
                   mid=(lo+high)/2;
                   //cout<<"mid "<<mid <<endl;
                      if((mid*q)>= y &&(mid*p)>=x)
                      {
                          if((mid*q)-y -((mid*p)-x)<=dif && (mid*p)-x <=(mid*q)-y)
                          {//cout<<"yes"<<endl;
                              dif=(mid*q)-y -((mid*p)-x);

                              ans=min(ans,(mid*q)-y);

                              high=mid;
                          }
                          else
                            lo=mid;
                      }
                      else
                      {
                          lo=mid;
                      }
                  }
                 // cout<<" lo is "<<lo <<"   h igh os "<<high<<endl;
                  if((high*q)-y -((high*p)-x)<=dif )
                          {
                              dif=(mid*q)-y -((mid*p)-x);
                              ans=min(ans,(high*q)-y);
                           //   cout<<"an 5 :"<<ans<<endl;
                          }
                 if((lo*q)-y -((lo*p)-x)<=dif && (lo*q)- y >=(lo*p)-x && (lo*q)- y >=0 &&(lo*p)-x >=0)
                          {

                              ans=min(ans,(lo*q)-y);
                            //  cout<<"an 577 :"<<ans<<endl;
                          }

                  cout<<ans<<endl;
       }

return 0;
}


#include<bits/stdc++.h>
#define ll unsigned long long int
using namespace std;
int main()
{ios::sync_with_stdio(0);
 ll t,p,q,x,y,i,j,cr,ans,num,dif,lo,mid,high;

 cin>>t;
    while(t--)
       {ans=dif=999999999999999999;
        cin>>x>>y>>p>>q;

        if((p==q &&x!=y)  || (p==0 &&x!=0) ||q==0 )
           {
           cout<<-1<<endl;
           continue;
           }
           if(p==0 &&x==0)
           {cout<<0<<endl;continue;}
          if(y%q==0)
             cr=y/q;
             else
             cr=(y/q)+1;
             lo=cr;
             high=9999999;

                  while(lo<high-1)
                  {
                   mid=(lo+high)/2;
                   //cout<<"mid "<<mid <<endl;
                      if((mid*q)>= y &&(mid*p)>=x)
                      {//cout<<" yoolo"<<endl;
                    //  cout<<(mid*q)-y<<endl;
                     // cout<<((mid*p)-x)<<endl;
                          if((mid*q)-y -((mid*p)-x)<=dif && (mid*p)-x <=(mid*q)-y)
                          {//cout<<"yes"<<endl;
                              dif=(mid*q)-y -((mid*p)-x);

                              ans=min(ans,(mid*q)-y);

                              high=mid;
                          }
                          else
                            lo=mid;
                      }
                      else
                      {
                          lo=mid;
                      }
                  }
                 // cout<<" lo is "<<lo <<"   h igh os "<<high<<endl;
                  if((high*q)-y -((high*p)-x)<=dif )
                          {
                              dif=(mid*q)-y -((mid*p)-x);
                              ans=min(ans,(high*q)-y);
                             //cout<<"an 5 :"<<ans<<endl;
                          }
                 if((lo*q)-y -((lo*p)-x)<=dif && (lo*q)- y >=(lo*p)-x && (lo*q)- y >=0 &&(lo*p)-x >=0)
                          {

                              ans=min(ans,(lo*q)-y);
                        //      cout<<"an 577 :"<<ans<<endl;
                          }

                  cout<<ans<<endl;
       }

return 0;
}


#include<bits/stdc++.h>
#define ll long long int
using namespace std;
int main()
{ios::sync_with_stdio(0);
 ll t,p,q,x,y,i,j,cr,ans,num,dif,lo,mid,high;

 cin>>t;
    while(t--)
       {ans=dif=999999999999;
        cin>>x>>y>>p>>q;

        if(p==q &&x!=y  )
           {
           cout<<-1<<endl;
           continue;
           }

          if(y%q==0)
             cr=y/q;
             else
             cr=(y/q)+1;
             lo=cr;
             high=999999999;

                  while(lo<high-1)
                  {
                   mid=(lo+high)/2;
                   //cout<<"mid "<<mid <<endl;
                      if((mid*q)>= y &&(mid*p)>=x)
                      {
                          if((mid*q)-y -((mid*p)-x)<=dif && (mid*p)-x <=(mid*q)-y)
                          {//cout<<"yes"<<endl;
                              dif=(mid*q)-y -((mid*p)-x);

                              ans=min(ans,(mid*q)-y);

                              high=mid;
                          }
                          else
                            lo=mid;
                      }
                      else
                      {
                          lo=mid;
                      }
                  }
                 // cout<<" lo is "<<lo <<"   h igh os "<<high<<endl;
                  if((high*q)-y -((high*p)-x)<=dif )
                          {
                              dif=(mid*q)-y -((mid*p)-x);
                              ans=min(ans,(high*q)-y);
                           //   cout<<"an 5 :"<<ans<<endl;
                          }
                 if((lo*q)-y -((lo*p)-x)<=dif && (lo*q)- y >=(lo*p)-x && (lo*q)- y >=0 &&(lo*p)-x >=0)
                          {

                              ans=min(ans,(lo*q)-y);
                            //  cout<<"an 577 :"<<ans<<endl;
                          }

                  cout<<ans<<endl;
       }

return 0;
}


#include<bits/stdc++.h>
#define ll long long int
using namespace std;
int main()
{ios::sync_with_stdio(0);
 ll t,p,q,x,y,i,j,cr,ans,num,dif,lo,mid,high;

 cin>>t;
    while(t--)
       {ans=dif=999999999999;
        cin>>x>>y>>p>>q;

        if((p==q &&x!=y)  || p==0 ||q==0 )
           {
           cout<<-1<<endl;
           continue;
           }

          if(y%q==0)
             cr=y/q;
             else
             cr=(y/q)+1;
             lo=cr;
             high=999999999;

                  while(lo<high-1)
                  {
                   mid=(lo+high)/2;
                   //cout<<"mid "<<mid <<endl;
                      if((mid*q)>= y &&(mid*p)>=x)
                      {
                          if((mid*q)-y -((mid*p)-x)<=dif && (mid*p)-x <=(mid*q)-y)
                          {//cout<<"yes"<<endl;
                              dif=(mid*q)-y -((mid*p)-x);

                              ans=min(ans,(mid*q)-y);

                              high=mid;
                          }
                          else
                            lo=mid;
                      }
                      else
                      {
                          lo=mid;
                      }
                  }
                 // cout<<" lo is "<<lo <<"   h igh os "<<high<<endl;
                  if((high*q)-y -((high*p)-x)<=dif )
                          {
                              dif=(mid*q)-y -((mid*p)-x);
                              ans=min(ans,(high*q)-y);
                           //   cout<<"an 5 :"<<ans<<endl;
                          }
                 if((lo*q)-y -((lo*p)-x)<=dif && (lo*q)- y >=(lo*p)-x && (lo*q)- y >=0 &&(lo*p)-x >=0)
                          {

                              ans=min(ans,(lo*q)-y);
                            //  cout<<"an 577 :"<<ans<<endl;
                          }

                  cout<<ans<<endl;
       }

return 0;
}


#include <bits/stdc++.h>
using namespace std;
typedef long long LL;
void gcd(LL a, LL b, LL &d, LL &x, LL &y)
{
    if (!b)
    {
        d = a;
        x = 1;
        y = 0;
    }
    else
    {
        gcd(b, a % b, d, y, x);
        y -= x * (a / b);
    }
}
int main()
{
    LL t, x, y, p, q, g, a, b;
    scanf("%lld", &t);
    while (t--)
    {
        scanf("%lld%lld%lld%lld", &x, &y, &p, &q);
        gcd(q - p, -p, g, a, b);
        LL c = (LL)p * y - (LL)q * x;

        if (q - p == 0)
        {
            if (c % p || c / -p < 0)
            {
                puts("-1");
                continue;
            }
            printf("%lld\n", c / -p);
            continue;
        }
        if (p == 0)
        {
            if (c % (q - p) || c / (q - p) < 0)
            {
                puts("-1");
                continue;
            }
            printf("%lld\n", c / (q - p));
            continue;
        }
        if (c % g)
        {
            puts("-1");
            continue;
        }

        //printf("a:%lld b:%lld g:%lld c:%lld q-p:%lld -p:%lld\n", a, b, g, c, q - p, -p);
        LL x0 = c / g * a;
        LL y0 = c / g * b;
        a = q - p;
        b = -p;
        LL aa = a / abs(g);
        LL bb = b / abs(g);

        LL mink = 0, maxk = 0;
        maxk = y0 / aa;
        if (bb > 0)
        {
            mink = -x0 / bb;
            if (maxk < mink)
            {
                puts("-1");
                continue;
            }
            if(x0+maxk*bb<0 || y-maxk*aa<0)
            {
                puts("-1");
                continue;
            }
        }
        else
        {
            maxk = min(maxk, -x0 / bb);
             if(x0+maxk*bb<0 || y-maxk*aa<0)
            {
                puts("-1");
                continue;
            }
        }
        if (bb - aa < 0)
            printf("%lld\n", x0 + y0 + maxk * (bb - aa));
        else
            printf("%lld\n", x0 + y0 + min(mink, maxk) * (bb - aa));
    }
    return 0;
}


#include <bits/stdc++.h>
using namespace std;
typedef long long LL;
void gcd(LL a, LL b, LL &d, LL &x, LL &y)
{
    if (!b)
    {
        d = a;
        x = 1;
        y = 0;
    }
    else
    {
        gcd(b, a % b, d, y, x);
        y -= x * (a / b);
    }
}
LL t, x, y, p, q, g, a, b, c;
LL dealZero(LL t)
{
    if (c % t || c / t < 0)
        return -1;
    return c / t;
}
LL divab(LL a,LL b)//整数相除向小的数取整（负数取整绝对值会变大)
{
    double x=double(a)/b;
    if (x>=0) return (LL)x;
    return (LL)(x-1+1e-9);
}
int main()
{
    scanf("%lld", &t);
    while (t--)
    {
        scanf("%lld%lld%lld%lld", &x, &y, &p, &q);
        //(x+a)/(y+a+b)=p/q     a=? b=? min(a+b)=?(a>=0 && b>=0)
        LL t1 = q - p, t2 = -p;
        //t1*a + t2*b = p*y - q*x
        c = (LL)p * y - (LL)q * x;
        if (t1 == 0)
        {
            printf("%lld\n", dealZero(t2));
            continue;
        }
        if (t2 == 0)
        {
            printf("%lld\n", dealZero(t1));
            continue;
        }
        gcd(t1, t2, g, a, b); //a*t1+b*t2=g
        if (g < 0)
            a = -a, b = -b, g = -g;
        if (c % g)
        {
            printf("-1\n");
            continue;
        }
        a *= c / g;
        b *= c / g;
        //a*t1+b*t2=c
        //a*t1+k*t1*t2/g + b*t2 - k*t1*t2/g = c     变动相差一个t1,t2的最小共倍数t1*t2/g
        //(a+k*t2/g)*t1 + (b-k*t1/g)*t2=c
        //a+k*t2/g>=0 && b-k*t1/g>=0
        //t2/g > 0 a>=-k*t2/g ==> a*g/t2>=-k ==> -a*g/t2<=k
        //t2/g < 0 a>=-k*t2/g ==> a*g/t2<=-k ==> -a*g/t2>=k
        //printf("a:%lld t1:%lld b:%lld t2:%lld g:%lld c:%lld\n", a, t1, b, t2, g, c);
        LL mink, maxk, hasMink = false, hasMaxk = false;
        t1/=g;
        t2/=g;
        if (t2 >= 0)
        {
            hasMink = true;
            mink = divab(-a,t2);
        }
        else
        {
            hasMaxk = true;
            maxk = divab(-a,t2);
        }
        //t1/g > 0 ==> b >= k*t1/g ==> b*g/t1 >= k
        //t1/g > 0 ==> b >= k*t1/g ==> b*g/t1 <= k
        LL tmp=divab(b,t1);
        if (t1 >= 0)
        {
            if (hasMaxk)
                maxk = min(maxk, tmp);//**************************若为负数要向小的数取整
            else
                maxk = tmp, hasMaxk = true;
        }
        else
        {
            if (hasMink)
                mink = max(mink, tmp);
            else
                mink = tmp, hasMink = true;
        }
        //a+k*t2 + b - k*t1=a+b+k*(t2-t1)
        if (hasMaxk && hasMink && maxk < mink)
        {
            printf("-1\n");
            continue;
        }
        //printf("maxk:%lld hasMaxk:%lld mink:%lld hasMink:%lld\n", maxk, hasMaxk, mink, hasMink);
        LL t = t2 - t1;
        if (t > 0)
        {
            if (hasMink)
            {
                printf("%lld\n", a + b + mink * t);
                continue;
            }
            if (hasMaxk)
            {
                printf("%lld\n", a + b + min(maxk, 0LL) * t);
                continue;
            }
        }
        else
        {
            if (hasMaxk)
            {
                printf("%lld\n", a + b + maxk * t);
                continue;
            }
            if (hasMink)
            {
                printf("%lld\n", a + b + max(mink, 0LL) * t);
                continue;
            }
        }
    }
    return 0;
}


#include <bits/stdc++.h>
using namespace std;
typedef long long LL;
void gcd(LL a, LL b, LL &d, LL &x, LL &y)
{
    if (!b)
    {
        d = a;
        x = 1;
        y = 0;
    }
    else
    {
        gcd(b, a % b, d, y, x);
        y -= x * (a / b);
    }
}
int main()
{
    LL t, x, y, p, q, g, a, b;
    scanf("%lld", &t);
    while (t--)
    {
        scanf("%lld%lld%lld%lld", &x, &y, &p, &q);
        gcd(q - p, -p, g, a, b);
        LL c = (LL)p * y - (LL)q * x;

        if (q - p == 0)
        {
            if (c % p || c / -p < 0)
            {
                puts("-1");
                continue;
            }
            printf("%lld\n", c / -p);
            continue;
        }
        if (p == 0)
        {
            if (c % (q - p) || c / (q - p) < 0)
            {
                puts("-1");
                continue;
            }
            printf("%lld\n", c / (q - p));
            continue;
        }
        if (c % g)
        {
            puts("-1");
            continue;
        }

        //printf("a:%lld b:%lld g:%lld c:%lld q-p:%lld -p:%lld\n", a, b, g, c, q - p, -p);
        LL x0 = c / g * a;
        LL y0 = c / g * b;
        a = q - p;
        b = -p;
        if (a < 0)
            a = -a, b = -b, c = -c;
        LL aa = a / abs(g);
        LL bb = b / abs(g);
        LL mink = 0, maxk = 0;
        if (bb > 0)
        {
            mink = -x0 / bb;
            maxk = y0 / aa;
            if (maxk < mink)
            {
                puts("-1");
                continue;
            }
        }
        else
        {
            maxk = -x0 / bb;
            maxk = min(maxk, y0 / aa);
        }
        printf("%lld\n", x0 + y0 + maxk * (bb - aa));
    }
    return 0;
}


#include <bits/stdc++.h>
using namespace std;
typedef long long LL;
void gcd(LL a, LL b, LL &d, LL &x, LL &y)
{
    if (!b)
    {
        d = a;
        x = 1;
        y = 0;
    }
    else
    {
        gcd(b, a % b, d, y, x);
        y -= x * (a / b);
    }
}
int main()
{
    LL t, x, y, p, q, g, a, b;
    scanf("%lld", &t);
    while (t--)
    {
        scanf("%lld%lld%lld%lld", &x, &y, &p, &q);
        gcd(q - p, -p, g, a, b);
        LL c = (LL)p * y - (LL)q * x;

        if (q - p == 0)
        {
            if (c % p || c / -p < 0)
            {
                puts("-1");
                continue;
            }
            printf("%lld\n", c / -p);
            continue;
        }
        if (p == 0)
        {
            if (c % (q - p) || c / (q - p) < 0)
            {
                puts("-1");
                continue;
            }
            printf("%lld\n", c / (q - p));
            continue;
        }
        if (c % g)
        {
            puts("-1");
            continue;
        }

        //printf("a:%lld b:%lld g:%lld c:%lld q-p:%lld -p:%lld\n", a, b, g, c, q - p, -p);
        LL x0 = c / g * a;
        LL y0 = c / g * b;
        a = q - p;
        b = -p;
        LL aa = a / abs(g);
        LL bb = b / abs(g);
        LL mink = 0, maxk = 0;
        if (bb > 0)
        {
            mink = -x0 / bb;
            maxk = y0 / aa;
            if (maxk < mink)
            {
                puts("-1");
                continue;
            }
        }
        else
        {
            maxk = -x0 / bb;
            maxk = min(maxk, y0 / aa);
        }
        if (bb - aa < 0)
            printf("%lld\n", x0 + y0 + maxk * (bb - aa));
        else
            printf("%lld\n", x0 + y0 + mink * (bb - aa));
    }
    return 0;
}


#include <bits/stdc++.h>
using namespace std;
typedef long long LL;
void gcd(LL a, LL b, LL &d, LL &x, LL &y)
{
    if (!b)
    {
        d = a;
        x = 1;
        y = 0;
    }
    else
    {
        gcd(b, a % b, d, y, x);
        y -= x * (a / b);
    }
}
LL t, x, y, p, q, g, a, b, c;
LL dealZero(LL t)
{
    if (c % t || c / t < 0)
        return -1;
    return c / t;
}
LL divab(LL a, LL b) //整数相除向小的数取整（负数取整绝对值会变大)
{
    double x = double(a) / b;
    if (x >= 0)
        return (LL)x;
    return (LL)(x - 1 + 1e-9);
}
int main()
{
    scanf("%lld", &t);
    while (t--)
    {
        scanf("%lld%lld%lld%lld", &x, &y, &p, &q);
        //(x+a)/(y+a+b)=p/q     a=? b=? min(a+b)=?(a>=0 && b>=0)
        LL t1 = q - p, t2 = -p;
        //t1*a + t2*b = p*y - q*x
        c = (LL)p * y - (LL)q * x;
        if (t1 == 0)
        {
            printf("%lld\n", dealZero(t2));
            continue;
        }
        if (t2 == 0)
        {
            printf("%lld\n", dealZero(t1));
            continue;
        }
        gcd(t1, t2, g, a, b); //a*t1+b*t2=g
        if (g < 0)
            a = -a, b = -b, g = -g;
        if (c % g)
        {
            printf("-1\n");
            continue;
        }
        a *= c / g;
        b *= c / g;
        //************************************这里a，b乘完后后面处理会爆longlong
        a = a % (t2 / g);   //将结果先缩小
        b = (c - t1 * a) / t2;
        //a*t1+b*t2=c
        //a*t1+k*t1*t2/g + b*t2 - k*t1*t2/g = c     变动相差一个t1,t2的最小共倍数t1*t2/g
        //(a+k*t2/g)*t1 + (b-k*t1/g)*t2=c
        //a+k*t2/g>=0 && b-k*t1/g>=0
        //t2/g > 0 a>=-k*t2/g ==> a*g/t2>=-k ==> -a*g/t2<=k
        //t2/g < 0 a>=-k*t2/g ==> a*g/t2<=-k ==> -a*g/t2>=k
        //printf("a:%lld t1:%lld b:%lld t2:%lld g:%lld c:%lld\n", a, t1, b, t2, g, c);
        LL mink, maxk, hasMink = false, hasMaxk = false;
        t1 /= g;
        t2 /= g;
        if (t2 >= 0)
        {
            hasMink = true;
            mink = divab(-a, t2);
        }
        else
        {
            hasMaxk = true;
            maxk = divab(-a, t2);
        }
        //t1/g > 0 ==> b >= k*t1/g ==> b*g/t1 >= k
        //t1/g > 0 ==> b >= k*t1/g ==> b*g/t1 <= k
        LL tmp = divab(b, t1);
        if (t1 >= 0)
        {
            if (hasMaxk)
                maxk = min(maxk, tmp); //**************************若为负数要向小的数取整
            else
                maxk = tmp, hasMaxk = true;
        }
        else
        {
            if (hasMink)
                mink = max(mink, tmp);
            else
                mink = tmp, hasMink = true;
        }
        //a+k*t2 + b - k*t1=a+b+k*(t2-t1)
        if (hasMaxk && hasMink && maxk < mink)
        {
            printf("-1\n");
            continue;
        }
        //printf("maxk:%lld hasMaxk:%lld mink:%lld hasMink:%lld\n", maxk, hasMaxk, mink, hasMink);
        LL t = t2 - t1;
        if (t > 0)
        {
            if (hasMink)
            {
                printf("%lld\n", a + b + mink * t);
                continue;
            }
            if (hasMaxk)
            {
                printf("%lld\n", a + b + min(maxk, 0LL) * t);
                continue;
            }
        }
        else
        {
            if (hasMaxk)
            {
                printf("%lld\n", a + b + maxk * t);
                continue;
            }
            if (hasMink)
            {
                printf("%lld\n", a + b + max(mink, 0LL) * t);
                continue;
            }
        }
    }
    return 0;
}


#include <bits/stdc++.h>
using namespace std;
typedef long long LL;
void gcd(LL a, LL b, LL &d, LL &x, LL &y)
{
    if (!b)
    {
        d = a;
        x = 1;
        y = 0;
    }
    else
    {
        gcd(b, a % b, d, y, x);
        y -= x * (a / b);
    }
}
LL t, x, y, p, q, g, a, b, c;
LL dealZero(LL t)
{
    if (c % t || c / t < 0)
        return -1;
    return c / t;
}
LL divab(LL a, LL b) //整数相除向小的数取整（负数取整绝对值会变大)
{
    double x = double(a) / b;
    if (x >= 0)
        return (LL)x;
    return (LL)(x - 1 + 1e-9);
}
int main()
{
    scanf("%lld", &t);
    while (t--)
    {
        scanf("%lld%lld%lld%lld", &x, &y, &p, &q);
        //(x+a)/(y+a+b)=p/q     a=? b=? min(a+b)=?(a>=0 && b>=0)
        LL t1 = q - p, t2 = -p;
        //t1*a + t2*b = p*y - q*x
        c = (LL)p * y - (LL)q * x;
        if (t1 == 0)
        {
            printf("%lld\n", dealZero(t2));
            continue;
        }
        if (t2 == 0)
        {
            printf("%lld\n", dealZero(t1));
            continue;
        }
        gcd(t1, t2, g, a, b); //a*t1+b*t2=g
        if (g < 0)
            a = -a, b = -b, g = -g;
        if (c % g)
        {
            printf("-1\n");
            continue;
        }
        a *= c / g % (t2 / g);
        //b *= c / g;
        //************************************这里a，b乘完后后面处理会爆longlong
        a = a % (t2 / g);          //将结果先缩小
        b = (c - t1 / g * a) / t2; //会爆longlong
        //a*t1+b*t2=c
        //a*t1+k*t1*t2/g + b*t2 - k*t1*t2/g = c     变动相差一个t1,t2的最小共倍数t1*t2/g
        //(a+k*t2/g)*t1 + (b-k*t1/g)*t2=c
        //a+k*t2/g>=0 && b-k*t1/g>=0
        //t2/g > 0 a>=-k*t2/g ==> a*g/t2>=-k ==> -a*g/t2<=k
        //t2/g < 0 a>=-k*t2/g ==> a*g/t2<=-k ==> -a*g/t2>=k
        //printf("a:%lld t1:%lld b:%lld t2:%lld g:%lld c:%lld\n", a, t1, b, t2, g, c);
        LL mink, maxk, hasMink = false, hasMaxk = false;
        t1 /= g;
        t2 /= g;
        if (t2 >= 0)
        {
            hasMink = true;
            mink = divab(-a, t2);
        }
        else
        {
            hasMaxk = true;
            maxk = divab(-a, t2);
        }
        //t1/g > 0 ==> b >= k*t1/g ==> b*g/t1 >= k
        //t1/g > 0 ==> b >= k*t1/g ==> b*g/t1 <= k
        LL tmp = divab(b, t1);
        if (t1 >= 0)
        {
            if (hasMaxk)
                maxk = min(maxk, tmp); //**************************若为负数要向小的数取整
            else
                maxk = tmp, hasMaxk = true;
        }
        else
        {
            if (hasMink)
                mink = max(mink, tmp);
            else
                mink = tmp, hasMink = true;
        }
        //a+k*t2 + b - k*t1=a+b+k*(t2-t1)
        if (hasMaxk && hasMink && maxk < mink)
        {
            printf("-1\n");
            continue;
        }
        //printf("maxk:%lld hasMaxk:%lld mink:%lld hasMink:%lld\n", maxk, hasMaxk, mink, hasMink);
        LL t = t2 - t1;
        if (t > 0)
        {
            if (hasMink)
            {
                printf("%lld\n", a + b + mink * t);
                continue;
            }
            if (hasMaxk)
            {
                printf("%lld\n", a + b + min(maxk, 0LL) * t);
                continue;
            }
        }
        else
        {
            if (hasMaxk)
            {
                printf("%lld\n", a + b + maxk * t);
                continue;
            }
            if (hasMink)
            {
                printf("%lld\n", a + b + max(mink, 0LL) * t);
                continue;
            }
        }
    }
    return 0;
}


#include <bits/stdc++.h>
using namespace std;
typedef long long LL;
void gcd(LL a, LL b, LL &d, LL &x, LL &y)
{
    if (!b)
    {
        d = a;
        x = 1;
        y = 0;
    }
    else
    {
        gcd(b, a % b, d, y, x);
        y -= x * (a / b);
    }
}
LL t, x, y, p, q, g, a, b, c;
LL dealZero(LL t)
{
    if (c % t || c / t < 0)
        return -1;
    return c / t;
}
int main()
{
    scanf("%lld", &t);
    while (t--)
    {
        scanf("%lld%lld%lld%lld", &x, &y, &p, &q);
        //(x+a)/(y+a+b)=p/q     a=? b=? min(a+b)=?(a>=0 && b>=0)
        LL t1 = q - p, t2 = -p;
        //t1*a + t2*b = p*y - q*x
        c = (LL)p * y - (LL)q * x;
        if (t1 == 0)
        {
            printf("%lld\n", dealZero(t2));
            continue;
        }
        if (t2 == 0)
        {
            printf("%lld\n", dealZero(t1));
            continue;
        }
        gcd(t1, t2, g, a, b); //a*t1+b*t2=g
        if (g < 0)
            a = -a, b = -b, g = -g;
        if (c % g)
        {
            printf("-1\n");
            continue;
        }
        a *= c / g;
        b *= c / g;
        //a*t1+b*t2=c
        //a*t1+k*t1*t2/g + b*t2 - k*t1*t2/g = c     变动相差一个t1,t2的最小共倍数t1*t2/g
        //(a+k*t2/g)*t1 + (b-k*t1/g)*t2=c
        //a+k*t2/g>=0 && b-k*t1/g>=0
        //t2/g > 0 a>=-k*t2/g ==> a*g/t2>=-k ==> -a*g/t2<=k
        //t2/g < 0 a>=-k*t2/g ==> a*g/t2<=-k ==> -a*g/t2>=k
        LL mink, maxk, hasMink = false, hasMaxk = false;
        if (t2 / g > 0)
        {
            hasMink = true;
            mink = -a * g / t2;
        }
        else
        {
            hasMaxk = true;
            maxk = -a * g / t2;
        }
        //t1/g > 0 ==> b >= k*t1/g ==> b*g/t1 >= k
        //t1/g > 0 ==> b >= k*t1/g ==> b*g/t1 <= k
        if (t1 / g > 0)
        {
            if (hasMaxk)
                maxk = min(maxk, b * g / t1);
            else
                maxk = b * g / t1, hasMaxk = true;
        }
        else
        {
            if (hasMink)
                mink = max(mink, b * g / t1);
            else
                mink = b * g / t1, hasMink = true;
        }
        //printf("a:%lld t1:%lld b:%lld t2:%lld g:%lld c:%lld\n", a, t1, b, t2, g, c);
        //a+k*t2 + b - k*t1=a+b+k*(t2-t1)
        if (hasMaxk && hasMink && maxk < mink)
        {
            printf("-1\n");
            continue;
        }
        //printf("maxk:%lld hasMaxk:%lld mink:%lld hasMink:%lld\n", maxk, hasMaxk, mink, hasMink);
        LL t = t2 - t1;
        if (t > 0)
        {
            if (hasMink)
            {
                printf("%lld\n", a + b + mink * t);
                continue;
            }
            if (hasMaxk)
            {
                printf("%lld\n", a + b + min(maxk, 0LL) * t);
                continue;
            }
        }
        else
        {
            if (hasMaxk)
            {
                printf("%lld\n", a + b + maxk * t);
                continue;
            }
            if (hasMink)
            {
                printf("%lld\n", a + b + max(mink, 0LL) * t);
                continue;
            }
        }
    }
    return 0;
}


#include <bits/stdc++.h>
using namespace std;
typedef long long LL;
void gcd(LL a, LL b, LL &d, LL &x, LL &y)
{
    if (!b)
    {
        d = a;
        x = 1;
        y = 0;
    }
    else
    {
        gcd(b, a % b, d, y, x);
        y -= x * (a / b);
    }
}
int main()
{
    LL t, x, y, p, q, g, a, b;
    scanf("%lld", &t);
    while (t--)
    {
        scanf("%lld%lld%lld%lld", &x, &y, &p, &q);
        gcd(q - p, -p, g, a, b);
        LL c = (LL)p * y - (LL)q * x;

        if (q - p == 0)
        {
            if (c % p || c / -p < 0)
            {
                puts("-1");
                continue;
            }
            printf("%lld\n", c / -p);
            continue;
        }
        if (p == 0)
        {
            if (c % (q - p) || c / (q - p) < 0)
            {
                puts("-1");
                continue;
            }
            printf("%lld\n", c / (q - p));
            continue;
        }
        if (c % g)
        {
            puts("-1");
            continue;
        }

        //printf("a:%lld b:%lld g:%lld c:%lld q-p:%lld -p:%lld\n", a, b, g, c, q - p, -p);
        LL x0 = c / g * a;
        LL y0 = c / g * b;
        a = q - p;
        b = -p;
        LL aa = a / abs(g);
        LL bb = b / abs(g);
        LL mink = 0, maxk = 0;
        if (bb > 0)
        {
            mink = -x0 / bb;
            maxk = y0 / aa;
            if (maxk < mink)
            {
                puts("-1");
                continue;
            }
        }
        else
        {
            maxk = -x0 / bb;
            maxk = min(maxk, y0 / aa);
        }
        if (bb - aa < 0)
            printf("%lld\n", x0 + y0 + maxk * (bb - aa));
        else
            printf("%lld\n", x0 + y0 + min(mink, maxk) * (bb - aa));
    }
    return 0;
}


#include <bits/stdc++.h>
using namespace std;
typedef long long LL;
void gcd(LL a, LL b, LL &d, LL &x, LL &y)
{
    if (!b)
    {
        d = a;
        x = 1;
        y = 0;
    }
    else
    {
        gcd(b, a % b, d, y, x);
        y -= x * (a / b);
    }
}
int main()
{
    LL t, x, y, p, q, g, a, b;
    scanf("%lld", &t);
    while (t--)
    {
        scanf("%lld%lld%lld%lld", &x, &y, &p, &q);
        gcd(q - p, -p, g, a, b);
        LL c = (LL)p * y - (LL)q * x;

        if (q - p == 0)
        {
            if (c % p || c / -p < 0)
            {
                puts("-1");
                continue;
            }
            printf("%lld\n", c / -p);
            continue;
        }
        if (p == 0)
        {
            if (c % (q - p) || c / (q - p) < 0)
            {
                puts("-1");
                continue;
            }
            printf("%lld\n", c / (q - p));
            continue;
        }
        if (c % g)
        {
            puts("-1");
            continue;
        }

        //printf("a:%lld b:%lld g:%lld c:%lld q-p:%lld -p:%lld\n", a, b, g, c, q - p, -p);
        LL x0 = c / g * a;
        LL y0 = c / g * b;
        a = q - p;
        LL aa = abs(a / g);
        b = -p;
        LL bb = abs(b / g);
        if (a < 0 && b < 0 && c < 0)
            a = -a, b = -b, c = -c;
        LL ans=-1;
        x0 = (x0 % bb + bb) % bb;
        y0 = (c-a*x0)/b;
        if(x0>=0 && y0 >=0) ans=x0+y0;
        y0 = (y0%aa+aa)%aa;
        x0=(c-b*y0)/a;
        if(x0>=0 && y0 >=0)
        {
            if (ans==-1) ans=x0+y0;
            else ans=min(ans,x0+y0);
        }
        printf("%lld\n", ans);
    }
    return 0;
}


#include<stdio.h>
#include<stdlib.h>
int main()
{
    int n,i;
    scanf("%d",&n);
    if (n==1)
        printf("a\n");
    else{
        printf("b");
        for(i=1;i<n;i++)
        {
            if(i%4==1 || i%4==2)
                printf("a");
            else
                printf("b");
        }
    }
}


#include<stdio.h>
char a[5]="aabb";
int main()
{
    int n,i;
    scanf("%d",&n);
    int count=0;
    for( i=0;i<n;i++)
    {
        printf("%c",a[i]);
        //count++;
    }
    printf("\n");
}


#include<stdio.h>
char a[5]="aabb";
int main()
{
    int n,i;
    scanf("%d",&n);
    int count=0;
    for( i=0;i<n;i++)
    {
        printf("%c",a[count]);
        count++;
    }
    printf("\n");
}


#include<stdio.h>
char a[5]="aabb";
int main()
{
    int n,i;
    scanf("%d",&n);
    int count=0;
    for( i=0;i<n;i++)
    {
        printf("%c",a[count%4]);
        count++;
    }
    printf("\n");
}


#include <stdio.h>

int main()
{
	int n, m, i;
	char str[] = "aabb";
	scanf("%d", &n);
	m = n / 4;
	while(m--)
	{
		printf("%s", str);
	}
	n %= 4;
	for(i=0;i<n;++i)
	{
		printf("%c", str[i]);
	}
	return 0;
}


#include <stdio.h>

int main()
{
	int n, m, i;
	char str[] = "aabbc";
	scanf("%d", &n);
	m = n / 5;
	while(m--)
	{
		printf("%s", str);
	}
	n %= 5;
	for(i=0;i<n;++i)
	{
		printf("%c", str[i]);
	}
	return 0;
}


#include<stdio.h>
int main() {
    long int a;
    char p[]="aabb";
    scanf("%ld",&a);
    if(a<=2)
        printf("aa\n");
    else if( a==3)
        printf("bba\n");
    else {
        int i;
        int l=a/4;
        for(i=0; i<l; i++)
            printf("aabb");
        if(a%4!=0)
            for(i=0; i<a%4; i++)
                printf("%c",p[i]);
    }
    printf("\n");
    return 0;
}


#include<stdio.h>
int main() {
    long int a;
    char p[]="aabb";
    scanf("%ld",&a);
    if(a==1) printf("a\n");
   else if(a<=2)
        printf("aa\n");
    else if( a==3)
        printf("bba\n");
    else {
        int i;
        int l=a/4;
        for(i=0; i<l; i++)
            printf("aabb");
        if(a%4!=0)
            for(i=0; i<a%4; i++)
                printf("%c",p[i]);
    }
    printf("\n");
    return 0;
}


/*		Programmer Name - Abhey Rana
		Institute - Motilal Nehru National Institute Of Technology
		Registration Number - 20154015
		Program Name - 3Palindrome
*/
#include <stdio.h>
#include <stdlib.h>
#include <limits.h>
#include <string.h>
#include <math.h>
#include <ctype.h>
#define max(a,b) ((a>b)?a:b)
#define min(a,b) ((a<b)?a:b)
int main(int argc,char *argv[]){
	int n;
	scanf("%d",&n);
	int i,j;
	char work[12] = {'b','b','a','a','b','b','a','a','b','b','a','a'};
	for(i=0;i<n-n%12;i+=12){
		for(j=0;j<12;j++)
			printf("%c",work[j]);
	}
	for(i=0;i<n%12;i++)
		printf("%c",work[i]);
}


/*		Programmer Name - Abhey Rana
		Institute - Motilal Nehru National Institute Of Technology
		Registration Number - 20154015
		Program Name - 3Palindrome
*/
#include <stdio.h>
#include <stdlib.h>
#include <limits.h>
#include <string.h>
#include <math.h>
#include <ctype.h>
#define max(a,b) ((a>b)?a:b)
#define min(a,b) ((a<b)?a:b)
int main(int argc,char *argv[]){
	int n;
	scanf("%d",&n);
	char result[n+1];
	int i,j;
	for(i=0;i<n-n%3;i+=3){
		result[i]='b';
		result[i+1]='b';
		result[i+2]='a';
	}
	if(n%3==0)
		result[i]='\0';
	else if(n%3==1){
		result[i]='a';
		result[i+1]='\0';
	}
	else if(n%3==2){
		result[i]='a';
		result[i+1]='b';
		result[i+2]='\0';
	}
	printf("%s\n",result);
}


#include<stdio.h>
int main()
{

    int  n,i;
    scanf("%d",&n);
    for(i=1;i<=n;i++)
    {
        if(i%4==1 || i%4==2)
            printf("b");
        else
            printf("a");

    }
}


#include<stdio.h>
int main()
{
    int  n,i;
    scanf("%d",&n);
    for(i=1;i<=n;i++){
        printf((i%4<=1)?"a":"b");
    }
}


#include <stdio.h>
#include <string.h>

int main()
{
long int n;
scanf("%ld",&n);

for(int i=0;i<n;i++)
{if(i%4==0||i%4==1)
    printf("a");
    else
    printf("b");

}

return 0;
}


#include<stdio.h>
int main(){
    int num;
    scanf("%d",&num);
    for(int i=0;i<num-1;i++){
        if(i%4==0 || i%4==1)
            printf("%c",'a');
        else
            printf("%c",'b')
    }
    if(i%4==0 || i%4==1)
        printf("%c\n",'c');
    else
        printf("%c\n",'b');
    return 0;
}


#include<stdio.h>
int main(){
    int num,i=0;
    scanf("%d",&num);
    for(;i<num-1;i++){
        if(i%4==0 || i%4==1)
            printf("%c",'a');
        else
            printf("%c",'b');
    }
    if(i%4==0 || i%4==1)
        printf("%c\n",'c');
    else
        printf("%c\n",'b');
    return 0;
}


#include<stdio.h>
int main(){
    int num,i=0;
    scanf("%d",&num);
    for(;i<num-1;i++){
        if(i%4==0 || i%4==1)
            printf("%c",'a');
        else
            printf("%c",'b');
    }
    if(i%4==0)
        printf("%c\n",'a');
    else if(i%4==1)
        printf("%c\n",'a');
    else if(i%4==2)
        printf("%c\n",'b');
    else
        printf("%c\n",'b');
    return 0;
}


#include<stdio.h>
int main(){
    int num,i=0;
    scanf("%d",&num);
    for(;i<num-1;i++){
        if(i%4==0 || i%4==1)
            printf("%c",'a');
        else
            printf("%c",'b');
    }
    if(num%2==0)
        if(i%4==0)
            printf("%c\n",'c');
        else
            printf("%c\n",'b');
    else
        if(i%4==0)
            printf("%c\n",'a');
        else
            printf("%c\n",'b');
    return 0;
}


#include<stdio.h>
int main(){
    int num,i=0;
    scanf("%d",&num);
    for(;i<num-1;i++){
        if(i%4==0 || i%4==1)
            printf("%c",'a');
        else
            printf("%c",'b')
    }
    if(i%4==0 || i%4==1)
        printf("%c\n",'c');
    else
        printf("%c\n",'b');
    return 0;
}


#include<stdio.h>
int main(){
    int num;
    scanf("%d",&num);
    for(int i=0;i<num-1;i++){
        printf("%c",'a');
    }
    printf("%c\n",'b');
    return 0;
}


#include<stdio.h>
int main(){
    int num,i=0;
    scanf("%d",&num);
    for(;i<num-1;i++){
        if(i%4==0 || i%4==1)
            printf("%c",'a');
        else
            printf("%c",'b');
    if(i%4==0)
        printf("%c\n",'a');
    else if(i%4==1)
        printf("%c\n",'a');
    else if(i%4==2)
        printf("%c\n",'b');
    else
        printf("%c\n",'b');
    return 0;
}


#include<stdio.h>
int main(){
    int num,i=0;
    scanf("%d",&num);
    for(;i<num-1;i++){
        if(i%4==0 || i%4==1)
            printf("%c",'a');
        else
            printf("%c",'b');
    }
    if(i%4==0)
        printf("%c\n",'c');
    else
        printf("%c\n",'b');
    return 0;
}


#include<stdio.h>
int main(){
    int num,i=0;
    scanf("%d",&num);
    for(;i<num-1;i++){
        if(i%4==0 || i%4==1)
            printf("%c",'a');
        else
            printf("%c",'b');
    if(i%4==0)
        printf("%c\n",'a');
    else if(i%4==1)
        printf("%c\n",'a');
    else if(i%4==2){
        printf("%c\n",'b');
    else
        printf("%c\n",'b');
    return 0;
}


#include <stdio.h>

int main()
{
    int n, i, d, r;

    scanf("%d", &n);

    d = n / 4;
    r = n % 4;

    for (i = 0; i < d; i++) {
        printf("aabb");
    }

    if (r == 1) {
        printf("a");
    } else if (r == 2) {
        printf("aa");
    } else if (r == 3) {
        printf("aab");
    }

    return 0;
}


#include <stdio.h>

int main()
{
  int a,i;
  scanf("%d",&a);
  for (i=0;i<a;i++){
    if (i%4==0)
      printf("a");
    if (i%4==1)
      printf("a");
    if (i%4==2)
      printf("b");
    if (i%4==3)
      printf("b");
  }
  printf("\n");
  return 0;
}


#include <stdio.h>

int main()
{
  int a,i;
  scanf("%d",&a);
  for (i=0;i<a;i++){
    if (i%4==0)
      printf("a");
    if (i%4==1)
      printf("a");
    if (i%4==2)
      printf("b");
    if (i%4==3)
      printf("c");
  }
  printf("\n");
  return 0;
}


#include <stdio.h>
int main() {
    char c[] = "aabb";
    int n;
    scanf("%d", &n);
    for(int i = 0; i < n; i++)
        putchar(c[i % 4]);
    return 0;
}


#include <bits/stdc++.h>
using namespace std;
int main() {
    int n;
    scanf("%d", &n);
    for(int i = 1; i < n - 1; i++)
        printf("a");
    printf("bc\n");
    return 0;
}


#include <stdio.h>
int main() {
    char c[] = {'a', 'a', 'b', 'b'};
    int n;
    scanf("%d", &n);
    for(int i = 0; i < n; i++)
        putchar(c[i % 4]);
    return 0;
}


#include<stdio.h>
int main()
{
    int i,n;
    scanf("%d",&n);
    for(i=1;i<=n;i++)
    {
        if(i<=n)
        {
            printf("a");
            i++;
        }
        if(i<=n)
        {
            printf("a");
            i++;
        }
        if(i<=n)
        {
            printf("b");
            i++;
        }
        if(i<=n)
        {
            printf("b");
        }
    }
    return 0;

}


#include<stdio.h>
int main()
{
    int i,n;
    scanf("%d",&n);
    for(i=1;i<=n;i++)
    {
        if(i<=n)
        {
            printf("a");
            i++;
        }
        if(i<=n)
        {
            printf("a");
            i++;
        }
        if(i<=n)
        {
            printf("b");
            i++;
        }
        if(i<=n)
        {
            printf("b");
            i++;
        }
    }
    return 0;

}


#include<stdio.h>
int main()
{
  int n,flag=0,i;
   scanf("%d",&n);
   for(i=0;i<n/2;i++)
   {
       if(i%2==0)
       {
           printf("aa");
           flag=0;
       }
       else
       {
           printf("bb");
           flag=1;
       }
   }
   if(n%2!=0)
   {
       if(flag==1)
           printf("a\n");
       else
           printf("b\n");
   }
   else
   {
       printf("\n");
   }
   return 0;
}


#include<stdio.h>
int main()
{
  int n,flag=0,i;
   scanf("%d",&n);
   for(i=0;i<n/2;i++)
   {
       if(i%2==0)
       {
           printf("aa");
       }
       else
       {
           printf("bb");
           flag=1;
       }
   }
   if(n%2!=0)
   {
       if(flag==1)
           printf("a\n");
       else
           printf("b\n");
   }
   else
   {
       printf("\n");
   }
   return 0;
}


#include <stdio.h>
#include <string.h>

char s[200002];
int main ()
{
    char i, ck = 0;
    int n;
    scanf("%d", &n);
    s[0] = 'a';
    for(i = 1; i < n; i += 2)
    {
        if(i < (n - 1))
        {
            if(ck == 0)
            {
                strcat(s, "bb");
                ck = 1;
            }
            else
            {
                strcat(s, "aa");
                ck = 0;
            }
        }
        else
        {
            if(ck == 0)
            {
                s[i] = 'b';
            }
            else
            {
                s[i] = 'a';
            }
        }
    }
    printf("%s\n", s);

    return 0;
}


#include<stdio.h>
main()
{
    int n,i=1;
    scanf("%d",&n);
    if(n%2==0)
    {
        for(i=1;i<=n/2;i++)
        {
            if(i%2==0)
                printf("aa");
            else
                printf("bb");
        }
    }
    else
    {
        for(i=1;i<=n/2;i++)
        {
            if(i%2==0)
                printf("aa");
            else
                printf("bb");
        }
        if(i%2==0)
                printf("a");
            else
                printf("b");
    }

}


#include<stdio.h>
main()
{
    int n,i;
    scanf("%d",&n);
    for(i=1;i<n;i++)
        printf("a");
    printf("b");
}


#include <stdio.h>
char C[4] = { 'a','a','b','b' };
int main() {
	int n; scanf("%d", &n);
	for (int i = 0; i < n; i++) {
		printf("%c", C[i % 4]);
	}
}


#include<stdio.h>
#include<stdlib.h>

int main()
{
    int len;
    char arr[len];
    scanf("%d",&len);
    if(len>=1) arr[0] = 'a';
    if(len>=2) arr[1] = 'b';
    for(int i=2;i<len;i++)
    {
        if(arr[i-1] == 'a')
        {
            if(arr[i-2]=='a') arr[i] = 'b';
            else arr[i] = 'a';
        }
        else{
            if(arr[i-2]=='b') arr[i] = 'a';
            else arr[i] = 'b';
        }
    }
    for(int i=0;i<len;i++) printf("%c",arr[i]);
    printf("\n");
    return 0;
}


#include<stdio.h>
#include<stdlib.h>

int main()
{
    char arr[3];
    arr[0] = 'a';
    arr[1] = 'b';
    arr[2] = 'c';
    int len;
    scanf("%d",&len);
    for(int i=0;i<len;i++)
    {
        printf("%c",arr[i%len]);
    }
    printf("\n");
    return 0;
}