/*input
4
3 10 1 2
7 14 3 8
20 70 2 7
5 6 1 1
*/
#include <bits/stdc++.h>
#define endl '\n'
#define fo(i,n) for(i=0;i<n;++i)
#define forr(i,n) for(i=n-1;i>=0;--i)
using namespace std;
typedef long long int ll;
typedef pair<int,int> ii;
typedef vector<ii> vii;
typedef vector<int> vi;

ll gcd (ll a, ll b, ll & x, ll & y) {
	if (a == 0) {
 x = 0; y = 1;
		return b;
	}
	ll x1, y1;
	ll d = gcd (b%a, a, x1, y1);
 x = y1 - (b / a) * x1;
 y = x1;
	return d;
}

int main(){
	ios_base::sync_with_stdio(0);
	cin.tie(NULL);
	cout.tie(NULL);
	int te;
	cin>>te;
	while(te--){
		ll s, t, p, q;
		cin>>s>>t>>p>>q;
		ll x, y;
		ll h = gcd(q-p,-p,x,y);
		if((p*t - q*s) % h != 0){
			cout<<-1<<endl;
			continue;
		}
		ll helik = (p*t-q*s)/h;
		x = x * helik;
		y = y * helik;
		ll h1 = p/h;
		ll h2 = (q-p)/h;
		if(x==0 && y==0){
			cout<<0<<endl;
			continue;
		}
		if(h1==0 && h2==0){
			if(x>0 && y>0){
				cout<<(x+y)<<endl;
				continue;
			}
			else{
				cout<<-1<<endl;
				continue;
			}
		}
		if(h1==0){
			if(x<0){
				cout<<-1<<endl;
				continue;
			}
			else if(y>0){
				ll sub = y/h2;
				y = y - sub*h2;
				cout<<(x+y)<<endl;
				continue;
			}
			else{
				ll sub = ceil((abs(y)*1.0)/(double(h2)));
				y = y + sub*h2;
				cout<<(x+y)<<endl;
				continue;
			}
		}
		else if(h2==0){
			if(y<0){
				cout<<-1<<endl;
				continue;
			}
			else if(x>0){
				ll sub = x/h1;
				x = x - sub*h1;
				cout<<(x+y)<<endl;
				continue;
			}
			else{
				ll sub = ceil((abs(x)*1.0)/(double(h1)));
				x = x + sub*h1;
				cout<<(x+y)<<endl;
				continue;
			}
		}
		// if(x==0 && y<0){
		// 	h1 = abs(h1);
		// 	h2 = abs(h2);
		// 	ll sub = ceil((abs(y)*1.0)/(double(h2)));
		// 	x = x + sub*h1;
		// 	y = y + sub*h2;
		// 	cout<<(x+y)<<endl;
		// 	continue;
		// }
		if(x==0 && y>0){
			cout<<y<<endl;
			continue;
		}
		// if(y==0 && x<0){
		// 	h1 = abs(h1);
		// 	h2 = abs(h2);
		// 	ll sub = ceil((abs(x)*1.0)/(double(h1)));
		// 	x = x + sub*h1;
		// 	y = y + sub*h2;
		// 	cout<<(x+y)<<endl;
		// 	continue;
		// }
		if(y==0 && x>0){
			cout<<x<<endl;
			continue;
		}
		if(y<0 && h2==0){
			cout<<-1<<endl;
			continue;
		}
		if(x>0 && y>0){
			h1 = abs(h1);
			h2 = abs(h2);
			ll t1 = x/h1;
			ll t2 = y/h2;
			ll sub = min(t1,t2);
			x = x - sub*h1;
			y = y - sub*h2;
			cout<<(x+y)<<endl;
			continue;
		}
		if(x>0 && y<0){
			h1 = abs(h1);
			h2 = abs(h2);
			ll sub = ceil((abs(y)*1.0)/(double(h2)));
			x = x + sub*h1;
			y = y + sub*h2;
			cout<<(x+y)<<endl;
			continue;
		}
		if(x<0 && y>0){
			h1 = abs(h1);
			h2 = abs(h2);
			ll sub = ceil((abs(x)*1.0)/(double(h1)));
			x = x + sub*h1;
			y = y + sub*h2;
			cout<<(x+y)<<endl;
			continue;
		}
		if(x>0 && y>0){
			// cout<<x<<" "<<y<<" "<<h1<<" "<<h2<<endl;
			h1 = abs(h1);
			h2 = abs(h2);
			ll t1 = ceil((abs(x)*1.0)/(double(h1)));
			ll t2 = ceil((abs(y)*1.0)/(double(h2)));
			ll sub = max(t1,t2);
			// cout<<sub<<endl;
			x = x + sub*h1;
			y = y + sub*h2;
			cout<<(x+y)<<endl;
			continue;
		}
	}
	return 0;
}


/*input
4
3 10 1 2
7 14 3 8
20 70 2 7
5 6 1 1
*/
#include <bits/stdc++.h>
#define endl '\n'
#define fo(i,n) for(i=0;i<n;++i)
#define forr(i,n) for(i=n-1;i>=0;--i)
using namespace std;
typedef long long int ll;
typedef pair<int,int> ii;
typedef vector<ii> vii;
typedef vector<int> vi;

ll gcd (ll a, ll b, ll & x, ll & y) {
	if (a == 0) {
 x = 0; y = 1;
		return b;
	}
	ll x1, y1;
	ll d = gcd (b%a, a, x1, y1);
 x = y1 - (b / a) * x1;
 y = x1;
	return d;
}

int main(){
	ios_base::sync_with_stdio(0);
	cin.tie(NULL);
	cout.tie(NULL);
	int te;
	cin>>te;
	while(te--){
		ll s, t, p, q;
		cin>>s>>t>>p>>q;
		ll x, y;
		ll h = gcd(q-p,-p,x,y);
		if((p*t - q*s) % h != 0){
			cout<<-1<<endl;
			continue;
		}
		ll helik = (p*t-q*s)/h;
		x = x * helik;
		y = y * helik;
		ll h1 = p/h;
		ll h2 = (q-p)/h;
		if(x==0 && y==0){
			cout<<0<<endl;
			continue;
		}
		if(x==0 && y>0){
			cout<<y<<endl;
			continue;
		}
		if(y==0 && x>0){
			cout<<x<<endl;
			continue;
		}
		if(h1==0 && h2==0){
			if(x>0 && y>0){
				cout<<(x+y)<<endl;
				continue;
			}
			else{
				cout<<-1<<endl;
				continue;
			}
		}
		if(h1==0){
			if(x<0){
				cout<<-1<<endl;
				continue;
			}
			else if(y>0){
				ll sub = y/h2;
				y = y - sub*h2;
				cout<<(x+y)<<endl;
				continue;
			}
			else{
				ll sub = ceil((abs(y)*1.0)/(double(h2)));
				y = y + sub*h2;
				cout<<(x+y)<<endl;
				continue;
			}
		}
		else if(h2==0){
			if(y<0){
				cout<<-1<<endl;
				continue;
			}
			else if(x>0){
				ll sub = x/h1;
				x = x - sub*h1;
				cout<<(x+y)<<endl;
				continue;
			}
			else{
				ll sub = ceil((abs(x)*1.0)/(double(h1)));
				x = x + sub*h1;
				cout<<(x+y)<<endl;
				continue;
			}
		}
		if(y<0 && h2==0){
			cout<<-1<<endl;
			continue;
		}
		if(x>0 && y>0){
			h1 = abs(h1);
			h2 = abs(h2);
			ll t1 = x/h1;
			ll t2 = y/h2;
			ll sub = min(t1,t2);
			x = x - sub*h1;
			y = y - sub*h2;
			cout<<(x+y)<<endl;
			continue;
		}
		if(x>0 && y<0){
			h1 = abs(h1);
			h2 = abs(h2);
			ll sub = ceil((abs(y)*1.0)/(double(h2)));
			x = x + sub*h1;
			y = y + sub*h2;
			cout<<(x+y)<<endl;
			continue;
		}
		if(x<0 && y>0){
			h1 = abs(h1);
			h2 = abs(h2);
			ll sub = ceil((abs(x)*1.0)/(double(h1)));
			x = x + sub*h1;
			y = y + sub*h2;
			cout<<(x+y)<<endl;
			continue;
		}
		if(x<0 && y<0){
			// cout<<x<<" "<<y<<" "<<h1<<" "<<h2<<endl;
			h1 = abs(h1);
			h2 = abs(h2);
			ll t1 = ceil((abs(x)*1.0)/(double(h1)));
			ll t2 = ceil((abs(y)*1.0)/(double(h2)));
			ll sub = max(t1,t2);
			// cout<<sub<<endl;
			x = x + sub*h1;
			y = y + sub*h2;
			cout<<(x+y)<<endl;
			continue;
		}
		else{
			cout<<-1<<endl;
			continue;
		}
	}
	return 0;
}


/*input
4
2 4 1 10
7 14 3 8
20 70 2 7
5 6 1 1
*/
#include <bits/stdc++.h>
#define endl '\n'
#define fo(i,n) for(i=0;i<n;++i)
#define forr(i,n) for(i=n-1;i>=0;--i)
using namespace std;
typedef long long int ll;
typedef pair<int,int> ii;
typedef vector<ii> vii;
typedef vector<int> vi;

ll gcd (ll a, ll b, ll & x, ll & y) {
	if (a == 0) {
 x = 0; y = 1;
		return b;
	}
	ll x1, y1;
	ll d = gcd (b%a, a, x1, y1);
 x = y1 - (b / a) * x1;
 y = x1;
	return d;
}

int main(){
	ios_base::sync_with_stdio(0);
	cin.tie(NULL);
	cout.tie(NULL);
	int te;
	cin>>te;
	while(te--){
		ll s, t, p, q;
		cin>>s>>t>>p>>q;
		ll x, y;
		ll h = gcd(q-p,-p,x,y);
		if((p*t - q*s) % h != 0){
			cout<<-1<<endl;
			continue;
		}
		ll helik = (p*t-q*s)/h;
		x = x * helik;
		y = y * helik;
		ll h1 = p/h;
		ll h2 = (q-p)/h;
		h1 = abs(h1);
		h2 = abs(h2);
		if(x==0 && y==0){
			cout<<0<<endl;
			continue;
		}
		if(h1==0 && h2==0){
			if(x>=0 && y>=0){
				cout<<(x+y)<<endl;
				continue;
			}
			else{
				cout<<-1<<endl;
				continue;
			}
		}
		if(h1==0){
			if(x<0){
				cout<<-1<<endl;
				continue;
			}
			else if(y>=0){
				ll sub = y/h2;
				y = y - sub*h2;
				cout<<(x+y)<<endl;
				continue;
			}
			else{
				ll sub = ceil((abs(y)*1.0)/(double(h2)));
				y = y + sub*h2;
				cout<<(x+y)<<endl;
				continue;
			}
		}
		else if(h2==0){
			if(y<0){
				cout<<-1<<endl;
				continue;
			}
			else if(x>=0){
				ll sub = x/h1;
				x = x - sub*h1;
				cout<<(x+y)<<endl;
				continue;
			}
			else{
				ll sub = ceil((abs(x)*1.0)/(double(h1)));
				x = x + sub*h1;
				cout<<(x+y)<<endl;
				continue;
			}
		}

		if(x==0 && y>=0){
			cout<<y<<endl;
			continue;
		}

		if(y==0 && x>=0){
			cout<<x<<endl;
			continue;
		}
		if(y<0 && h2==0){
			cout<<-1<<endl;
			continue;
		}
		if(x>=0 && y>=0){
			h1 = abs(h1);
			h2 = abs(h2);
			ll t1 = x/h1;
			ll t2 = y/h2;
			ll sub = min(t1,t2);
			x = x - sub*h1;
			y = y - sub*h2;
			cout<<(x+y)<<endl;
			continue;
		}
		if(x>=0 && y<0){
			h1 = abs(h1);
			h2 = abs(h2);
			ll sub = ceil((abs(y)*1.0)/(double(h2)));
			x = x + sub*h1;
			y = y + sub*h2;
			cout<<(x+y)<<endl;
			continue;
		}
		if(x<0 && y>=0){
			h1 = abs(h1);
			h2 = abs(h2);
			ll sub = ceil((abs(x)*1.0)/(double(h1)));
			x = x + sub*h1;
			y = y + sub*h2;
			cout<<(x+y)<<endl;
			continue;
		}
		if(x<0 && y<0){
			// cout<<x<<" "<<y<<" "<<h1<<" "<<h2<<endl;
			h1 = abs(h1);
			h2 = abs(h2);
			ll t1 = ceil((abs(x)*1.0)/(double(h1)));
			ll t2 = ceil((abs(y)*1.0)/(double(h2)));
			ll sub = max(t1,t2);
			// cout<<sub<<endl;
			x = x + sub*h1;
			y = y + sub*h2;
			cout<<(x+y)<<endl;
			continue;
		}
	}
	return 0;
}


/*input
4
3 10 1 2
7 14 3 8
20 70 2 7
5 6 1 1
*/
#include <bits/stdc++.h>
#define endl '\n'
#define fo(i,n) for(i=0;i<n;++i)
#define forr(i,n) for(i=n-1;i>=0;--i)
using namespace std;
typedef long long int ll;
typedef pair<int,int> ii;
typedef vector<ii> vii;
typedef vector<int> vi;

ll gcd (ll a, ll b, ll & x, ll & y) {
	if (a == 0) {
 x = 0; y = 1;
		return b;
	}
	ll x1, y1;
	ll d = gcd (b%a, a, x1, y1);
 x = y1 - (b / a) * x1;
 y = x1;
	return d;
}

int main(){
	ios_base::sync_with_stdio(0);
	cin.tie(NULL);
	cout.tie(NULL);
	int te;
	cin>>te;
	while(te--){
		ll s, t, p, q;
		cin>>s>>t>>p>>q;
		ll x, y;
		ll h = gcd(q-p,-p,x,y);
		if((p*t - q*s) % h != 0){
			cout<<-1<<endl;
			return 0;
		}
		ll helik = (p*t-q*s)/h;
		x = x * helik;
		y = y * helik;
		ll h1 = p/h;
		ll h2 = (q-p)/h;
		// cout<<x<<" "<<y<<" "<<h1<<" "<<h2<<endl;
		if(x==0 && y==0){
			cout<<0<<endl;
			continue;
		}
		else if(x==0 && y>0){
			cout<<y<<endl;
			continue;
		}
		else if(y==0 && x>0){
			cout<<x<<endl;
			continue;
		}
		else if(h1==0 && h2==0){
			if(x>0 && y>0){
				cout<<(x+y)<<endl;
				continue;
			}
			else{
				cout<<-1<<endl;
				continue;
			}
		}
		else if(h1==0){
			if(x<0){
				cout<<-1<<endl;
				continue;
			}
			else if(y>0){
				ll sub = y/h2;
				y = y - sub*h2;
				cout<<(x+y)<<endl;
				continue;
			}
			else{
				ll sub = ceil((abs(y)*1.0)/(double(h2)));
				y = y + sub*h2;
				cout<<(x+y)<<endl;
				continue;
			}
		}
		else if(h2==0){
			if(y<0){
				cout<<-1<<endl;
				continue;
			}
			else if(x>0){
				ll sub = x/h1;
				x = x - sub*h1;
				cout<<(x+y)<<endl;
				continue;
			}
			else{
				ll sub = ceil((abs(x)*1.0)/(double(h1)));
				x = x + sub*h1;
				cout<<(x+y)<<endl;
				continue;
			}
		}
		else if(y<0 && h2==0){
			cout<<-1<<endl;
			continue;
		}
		else if(x>0 && y>0){
			h1 = abs(h1);
			h2 = abs(h2);
			ll t1 = x/h1;
			ll t2 = y/h2;
			ll sub = min(t1,t2);
			x = x - sub*h1;
			y = y - sub*h2;
			cout<<(x+y)<<endl;
			continue;
		}
		else if(x>0 && y<0){
			h1 = abs(h1);
			h2 = abs(h2);
			ll sub = ceil((abs(y)*1.0)/(double(h2)));
			x = x + sub*h1;
			y = y + sub*h2;
			cout<<(x+y)<<endl;
			continue;
		}
		else if(x<0 && y>0){
			h1 = abs(h1);
			h2 = abs(h2);
			ll sub = ceil((abs(x)*1.0)/(double(h1)));
			x = x + sub*h1;
			y = y + sub*h2;
			cout<<(x+y)<<endl;
			continue;
		}
		else{
			// cout<<x<<" "<<y<<" "<<h1<<" "<<h2<<endl;
			h1 = abs(h1);
			h2 = abs(h2);
			ll t1 = ceil((abs(x)*1.0)/(double(h1)));
			ll t2 = ceil((abs(y)*1.0)/(double(h2)));
			ll sub = max(t1,t2);
			// cout<<sub<<endl;
			x = x + sub*h1;
			y = y + sub*h2;
			cout<<(x+y)<<endl;
			continue;
		}
	}
	return 0;
}


/*input
4
3 10 1 2
7 14 3 8
20 70 2 7
5 6 1 1
*/
//IT TAKES EVERYTHING
#include <bits/stdc++.h>
using namespace std;

#define endl '\n'
#define fo(i,n) for(i=0;i<n;++i)
#define forr(i,n) for(i=n-1;i>=0;--i)

#define INF (int)1e9
#define EPS 1e-9

#define sz(x) (int)(x).size()
#define first fi
#define second se
#define mp make_pair
#define pb push_back
#define yolo "debug_statement"

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

int binary(int x, int y, int p, int q){
	int tlow = 0;
	int thigh = y; //taking it as 10^9 also works since y <= 10^9
	int tmid;
	int res = -1;
	while(tlow <= thigh){
		tmid = (tlow+thigh)>>1;

		if(p*tmid>=x && (q*tmid-p*tmid)>=(y-x)){
			res = tmid;
			thigh = tmid - 1;
		}
		else
			tlow = tmid + 1;
	}
	return res;
}

int main(){
	int t, x, y, p, q;
	scanf("%d",&t);
	while(t--){
		scanf("%d%d%d%d",&x,&y,&p,&q);
		int ans = binary(x,y,p,q);
		if(ans==-1){
			printf("-1\n");
			continue;
		}
		printf("%d\n",q*ans - y);
	}
	return 0;
}


/*input
4
3 10 1 2
7 14 3 8
20 70 2 7
5 6 1 1
*/
#include <bits/stdc++.h>
#define endl '\n'
#define fo(i,n) for(i=0;i<n;++i)
#define forr(i,n) for(i=n-1;i>=0;--i)
using namespace std;
typedef long long int ll;
typedef pair<int,int> ii;
typedef vector<ii> vii;
typedef vector<int> vi;

ll gcd (ll a, ll b, ll & x, ll & y) {
	if (a == 0) {
 x = 0; y = 1;
		return b;
	}
	ll x1, y1;
	ll d = gcd (b%a, a, x1, y1);
 x = y1 - (b / a) * x1;
 y = x1;
	return d;
}

int main(){
	ios_base::sync_with_stdio(0);
	cin.tie(NULL);
	cout.tie(NULL);
	int te;
	cin>>te;
	while(te--){
		ll s, t, p, q;
		cin>>s>>t>>p>>q;
		ll x, y;
		ll h = gcd(q-p,-p,x,y);
		if(h==0){
			cout<<-1<<endl;
			continue;
		}
		if((p*t - q*s) % h != 0){
			cout<<-1<<endl;
			continue;
		}
		ll helik = (p*t-q*s)/h;
		x = x * helik;
		y = y * helik;
		ll h1 = p/h;
		ll h2 = (q-p)/h;
		if(x==0 && y==0){
			cout<<0<<endl;
			continue;
		}
		if(h1==0 && h2==0){
			if(x>=0 && y>=0){
				cout<<(x+y)<<endl;
				continue;
			}
			else{
				cout<<-1<<endl;
				continue;
			}
		}
		if(h1==0){
			if(x<0){
				cout<<-1<<endl;
				continue;
			}
			else if(y>=0){
				ll sub = y/h2;
				y = y - sub*h2;
				cout<<(x+y)<<endl;
				continue;
			}
			else{
				ll sub = ceil((abs(y)*1.0)/(double(h2)));
				y = y + sub*h2;
				cout<<(x+y)<<endl;
				continue;
			}
		}
		else if(h2==0){
			if(y<0){
				cout<<-1<<endl;
				continue;
			}
			else if(x>=0){
				ll sub = x/h1;
				x = x - sub*h1;
				cout<<(x+y)<<endl;
				continue;
			}
			else{
				ll sub = ceil((abs(x)*1.0)/(double(h1)));
				x = x + sub*h1;
				cout<<(x+y)<<endl;
				continue;
			}
		}

		if(x==0 && y>=0){
			cout<<y<<endl;
			continue;
		}

		if(y==0 && x>=0){
			cout<<x<<endl;
			continue;
		}
		if(y<0 && h2==0){
			cout<<-1<<endl;
			continue;
		}
		if(x>=0 && y>=0){
			h1 = abs(h1);
			h2 = abs(h2);
			ll t1 = x/h1;
			ll t2 = y/h2;
			ll sub = min(t1,t2);
			x = x - sub*h1;
			y = y - sub*h2;
			cout<<(x+y)<<endl;
			continue;
		}
		if(x>=0 && y<0){
			h1 = abs(h1);
			h2 = abs(h2);
			ll sub = ceil((abs(y)*1.0)/(double(h2)));
			x = x + sub*h1;
			y = y + sub*h2;
			cout<<(x+y)<<endl;
			continue;
		}
		if(x<0 && y>=0){
			h1 = abs(h1);
			h2 = abs(h2);
			ll sub = ceil((abs(x)*1.0)/(double(h1)));
			x = x + sub*h1;
			y = y + sub*h2;
			cout<<(x+y)<<endl;
			continue;
		}
		if(x<0 && y<0){
			// cout<<x<<" "<<y<<" "<<h1<<" "<<h2<<endl;
			h1 = abs(h1);
			h2 = abs(h2);
			ll t1 = ceil((abs(x)*1.0)/(double(h1)));
			ll t2 = ceil((abs(y)*1.0)/(double(h2)));
			ll sub = max(t1,t2);
			// cout<<sub<<endl;
			x = x + sub*h1;
			y = y + sub*h2;
			cout<<(x+y)<<endl;
			continue;
		}
	}
	return 0;
}


/*input
1
1 1 1 9
*/
#include <bits/stdc++.h>
#define endl '\n'
#define fo(i,n) for(i=0;i<n;++i)
#define forr(i,n) for(i=n-1;i>=0;--i)
using namespace std;
typedef long long int ll;
typedef pair<int,int> ii;
typedef vector<ii> vii;
typedef vector<int> vi;

ll gcd (ll a, ll b, ll & x, ll & y) {
	if (a == 0) {
 x = 0; y = 1;
		return b;
	}
	ll x1, y1;
	ll d = gcd (b%a, a, x1, y1);
 x = y1 - (b / a) * x1;
 y = x1;
	return d;
}

int main(){
	ios_base::sync_with_stdio(0);
	cin.tie(NULL);
	cout.tie(NULL);
	int te;
	cin>>te;
	while(te--){
		ll s, t, p, q;
		cin>>s>>t>>p>>q;
		ll x, y;
		ll h = gcd(q-p,-p,x,y);
		// if((p*t - q*s) % h != 0){
		// 	cout<<-1<<endl;
		// 	return 0;
		// }
		ll helik = (p*t-q*s)/h;
		x = x * helik;
		y = y * helik;
		ll h1 = p/h;
		ll h2 = (q-p)/h;
		// cout<<x<<" "<<y<<" "<<h1<<" "<<h2<<endl;
		if(x==0 && y==0){
			if(x<0 || y<0){
				cout<<-1<<endl;
				continue;
			}
			cout<<0<<endl;
			continue;
		}
		else if(x==0 && y>0){
			if(x<0 || y<0){
				cout<<-1<<endl;
				continue;
			}
			cout<<y<<endl;
			continue;
		}
		else if(y==0 && x>0){
			if(x<0 || y<0){
				cout<<-1<<endl;
				continue;
			}
			cout<<x<<endl;
			continue;
		}
		else if(h1==0 && h2==0){
			if(x>0 && y>0){
				if(x<0 || y<0){
					cout<<-1<<endl;
					continue;
				}
				cout<<(x+y)<<endl;
				continue;
			}
			else{
				if(x<0 || y<0){
					cout<<-1<<endl;
					continue;
				}
				cout<<-1<<endl;
				continue;
			}
		}
		else if(h1==0){
			if(x<0){
				if(x<0 || y<0){
					cout<<-1<<endl;
					continue;
				}
				cout<<-1<<endl;
				continue;
			}
			else if(y>0){
				if(x<0 || y<0){
					cout<<-1<<endl;
					continue;
				}
				cout<<(x+y)<<endl;
				continue;
			}
			else{
				ll sub = ceil((abs(y)*1.0)/(double(h2)));
				y = y + sub*h2;
				if(x<0 || y<0){
					cout<<-1<<endl;
					continue;
				}
				cout<<(x+y)<<endl;
				continue;
			}
		}
		else if(h2==0){
			if(y<0){
				if(x<0 || y<0){
					cout<<-1<<endl;
					continue;
				}
				cout<<-1<<endl;
				continue;
			}
			else if(x>0){
				if(x<0 || y<0){
					cout<<-1<<endl;
					continue;
				}
				cout<<(x+y)<<endl;
				continue;
			}
			else{
				ll sub = ceil((abs(x)*1.0)/(double(h1)));
				x = x + sub*h1;
				if(x<0 || y<0){
					cout<<-1<<endl;
					continue;
				}
				cout<<(x+y)<<endl;
				continue;
			}
		}
		else if(y<0 && h2==0){
			cout<<-1<<endl;
			continue;
		}
		else if(x>0 && y>0){
			h1 = abs(h1);
			h2 = abs(h2);
			ll t1 = x/h1;
			ll t2 = y/h2;
			ll sub = min(t1,t2);
			x = x - sub*h1;
			y = y - sub*h2;
			if(x<0 || y<0){
				cout<<-1<<endl;
				continue;
			}
			cout<<(x+y)<<endl;
			continue;
		}
		else if(x>0 && y<0){
			h1 = abs(h1);
			h2 = abs(h2);
			ll sub = ceil((abs(y)*1.0)/(double(h2)));
			x = x + sub*h1;
			y = y + sub*h2;
			if(x<0 || y<0){
				cout<<-1<<endl;
				continue;
			}
			cout<<(x+y)<<endl;
			continue;
		}
		else if(x<0 && y>0){
			h1 = abs(h1);
			h2 = abs(h2);
			ll sub = ceil((abs(x)*1.0)/(double(h1)));
			x = x + sub*h1;
			y = y + sub*h2;
			if(x<0 || y<0){
				cout<<-1<<endl;
				continue;
			}
			cout<<(x+y)<<endl;
			continue;
		}
		else{
			// cout<<x<<" "<<y<<" "<<h1<<" "<<h2<<endl;
			h1 = abs(h1);
			h2 = abs(h2);
			ll t1 = ceil((abs(x)*1.0)/(double(h1)));
			ll t2 = ceil((abs(y)*1.0)/(double(h2)));
			ll sub = max(t1,t2);
			// cout<<sub<<endl;
			x = x + sub*h1;
			y = y + sub*h2;
			if(x<0 || y<0){
				cout<<-1<<endl;
				continue;
			}
			cout<<(x+y)<<endl;
			continue;
		}
	}
	return 0;
}


/*input
4
3 10 1 2
7 14 3 8
20 70 2 7
5 6 1 1
*/
#include <bits/stdc++.h>
#define endl '\n'
#define fo(i,n) for(i=0;i<n;++i)
#define forr(i,n) for(i=n-1;i>=0;--i)
using namespace std;
typedef long long int ll;
typedef pair<int,int> ii;
typedef vector<ii> vii;
typedef vector<int> vi;

ll gcd (ll a, ll b, ll & x, ll & y) {
	if (a == 0) {
 x = 0; y = 1;
		return b;
	}
	ll x1, y1;
	ll d = gcd (b%a, a, x1, y1);
 x = y1 - (b / a) * x1;
 y = x1;
	return d;
}

int main(){
	ios_base::sync_with_stdio(0);
	cin.tie(NULL);
	cout.tie(NULL);
	int te;
	cin>>te;
	while(te--){
		ll s, t, p, q;
		cin>>s>>t>>p>>q;
		ll x, y;
		ll h = gcd(q-p,-p,x,y);
		if((p*t - q*s) % h != 0){
			cout<<-1<<endl;
			continue;
		}
		ll helik = (p*t-q*s)/h;
		x = x * helik;
		y = y * helik;
		ll h1 = p/h;
		ll h2 = (q-p)/h;
		if(x==0 && y==0){
			cout<<0<<endl;
			continue;
		}
		if(x==0 && y>0){
			cout<<y<<endl;
			continue;
		}
		if(y==0 && x>0){
			cout<<x<<endl;
			continue;
		}
		if(h1==0 && h2==0){
			if(x>0 && y>0){
				cout<<(x+y)<<endl;
				continue;
			}
			else{
				cout<<-1<<endl;
				continue;
			}
		}
		if(h1==0){
			if(x<0){
				cout<<-1<<endl;
				continue;
			}
			else if(y>0){
				ll sub = y/h2;
				y = y - sub*h2;
				cout<<(x+y)<<endl;
				continue;
			}
			else{
				ll sub = ceil((abs(y)*1.0)/(double(h2)));
				y = y + sub*h2;
				cout<<(x+y)<<endl;
				continue;
			}
		}
		else if(h2==0){
			if(y<0){
				cout<<-1<<endl;
				continue;
			}
			else if(x>0){
				ll sub = x/h1;
				x = x - sub*h1;
				cout<<(x+y)<<endl;
				continue;
			}
			else{
				ll sub = ceil((abs(x)*1.0)/(double(h1)));
				x = x + sub*h1;
				cout<<(x+y)<<endl;
				continue;
			}
		}
		if(y<0 && h2==0){
			cout<<-1<<endl;
			continue;
		}
		if(x>0 && y>0){
			h1 = abs(h1);
			h2 = abs(h2);
			ll t1 = x/h1;
			ll t2 = y/h2;
			ll sub = min(t1,t2);
			x = x - sub*h1;
			y = y - sub*h2;
			cout<<(x+y)<<endl;
			continue;
		}
		if(x>0 && y<0){
			h1 = abs(h1);
			h2 = abs(h2);
			ll sub = ceil((abs(y)*1.0)/(double(h2)));
			x = x + sub*h1;
			y = y + sub*h2;
			cout<<(x+y)<<endl;
			continue;
		}
		if(x<0 && y>0){
			h1 = abs(h1);
			h2 = abs(h2);
			ll sub = ceil((abs(x)*1.0)/(double(h1)));
			x = x + sub*h1;
			y = y + sub*h2;
			cout<<(x+y)<<endl;
			continue;
		}
		if(x<0 && y<0){
			// cout<<x<<" "<<y<<" "<<h1<<" "<<h2<<endl;
			h1 = abs(h1);
			h2 = abs(h2);
			ll t1 = ceil((abs(x)*1.0)/(double(h1)));
			ll t2 = ceil((abs(y)*1.0)/(double(h2)));
			ll sub = max(t1,t2);
			// cout<<sub<<endl;
			x = x + sub*h1;
			y = y + sub*h2;
			cout<<(x+y)<<endl;
			continue;
		}
		else{
			cout<<(x+y)<<endl;
			continue;
		}
	}
	return 0;
}


/*input
4
3 10 1 2
7 14 3 8
20 70 2 7
5 6 1 1
*/
#include <bits/stdc++.h>
#define endl '\n'
#define fo(i,n) for(i=0;i<n;++i)
#define forr(i,n) for(i=n-1;i>=0;--i)
using namespace std;
typedef long long int ll;
typedef pair<int,int> ii;
typedef vector<ii> vii;
typedef vector<int> vi;

ll gcd (ll a, ll b, ll & x, ll & y) {
	if (a == 0) {
 x = 0; y = 1;
		return b;
	}
	ll x1, y1;
	ll d = gcd (b%a, a, x1, y1);
 x = y1 - (b / a) * x1;
 y = x1;
	return d;
}

int main(){
	ios_base::sync_with_stdio(0);
	cin.tie(NULL);
	cout.tie(NULL);
	int te;
	cin>>te;
	while(te--){
		ll s, t, p, q;
		cin>>s>>t>>p>>q;
		ll x, y;
		ll h = gcd(q-p,-p,x,y);
		if(h==0){
			cout<<0<<endl;
			continue;
		}
		if((p*t - q*s) % h != 0){
			cout<<-1<<endl;
			continue;
		}
		ll helik = (p*t-q*s)/h;
		x = x * helik;
		y = y * helik;
		ll h1 = p/h;
		ll h2 = (q-p)/h;
		if(x==0 && y==0){
			cout<<0<<endl;
			continue;
		}
		if(h1==0 && h2==0){
			if(x>=0 && y>=0){
				cout<<(x+y)<<endl;
				continue;
			}
			else{
				cout<<-1<<endl;
				continue;
			}
		}
		if(h1==0){
			if(x<0){
				cout<<-1<<endl;
				continue;
			}
			else if(y>=0){
				ll sub = y/h2;
				y = y - sub*h2;
				cout<<(x+y)<<endl;
				continue;
			}
			else{
				ll sub = ceil((abs(y)*1.0)/(double(h2)));
				y = y + sub*h2;
				cout<<(x+y)<<endl;
				continue;
			}
		}
		else if(h2==0){
			if(y<0){
				cout<<-1<<endl;
				continue;
			}
			else if(x>=0){
				ll sub = x/h1;
				x = x - sub*h1;
				cout<<(x+y)<<endl;
				continue;
			}
			else{
				ll sub = ceil((abs(x)*1.0)/(double(h1)));
				x = x + sub*h1;
				cout<<(x+y)<<endl;
				continue;
			}
		}

		if(x==0 && y>=0){
			cout<<y<<endl;
			continue;
		}

		if(y==0 && x>=0){
			cout<<x<<endl;
			continue;
		}
		if(y<0 && h2==0){
			cout<<-1<<endl;
			continue;
		}
		if(x>=0 && y>=0){
			h1 = abs(h1);
			h2 = abs(h2);
			ll t1 = x/h1;
			ll t2 = y/h2;
			ll sub = min(t1,t2);
			x = x - sub*h1;
			y = y - sub*h2;
			cout<<(x+y)<<endl;
			continue;
		}
		if(x>=0 && y<0){
			h1 = abs(h1);
			h2 = abs(h2);
			ll sub = ceil((abs(y)*1.0)/(double(h2)));
			x = x + sub*h1;
			y = y + sub*h2;
			cout<<(x+y)<<endl;
			continue;
		}
		if(x<0 && y>=0){
			h1 = abs(h1);
			h2 = abs(h2);
			ll sub = ceil((abs(x)*1.0)/(double(h1)));
			x = x + sub*h1;
			y = y + sub*h2;
			cout<<(x+y)<<endl;
			continue;
		}
		if(x<0 && y<0){
			// cout<<x<<" "<<y<<" "<<h1<<" "<<h2<<endl;
			h1 = abs(h1);
			h2 = abs(h2);
			ll t1 = ceil((abs(x)*1.0)/(double(h1)));
			ll t2 = ceil((abs(y)*1.0)/(double(h2)));
			ll sub = max(t1,t2);
			// cout<<sub<<endl;
			x = x + sub*h1;
			y = y + sub*h2;
			cout<<(x+y)<<endl;
			continue;
		}
	}
	return 0;
}


/*input
4
3 10 1 2
7 14 3 8
20 70 2 7
5 6 1 1
*/
#include <bits/stdc++.h>
#define endl '\n'
#define fo(i,n) for(i=0;i<n;++i)
#define forr(i,n) for(i=n-1;i>=0;--i)
using namespace std;
typedef long long int ll;
typedef pair<int,int> ii;
typedef vector<ii> vii;
typedef vector<int> vi;

ll gcd (ll a, ll b, ll & x, ll & y) {
	if (a == 0) {
 x = 0; y = 1;
		return b;
	}
	ll x1, y1;
	ll d = gcd (b%a, a, x1, y1);
 x = y1 - (b / a) * x1;
 y = x1;
	return d;
}

int main(){
	ios_base::sync_with_stdio(0);
	cin.tie(NULL);
	cout.tie(NULL);
	int te;
	cin>>te;
	while(te--){
		ll s, t, p, q;
		cin>>s>>t>>p>>q;
		ll x, y;
		ll h = gcd(q-p,-p,x,y);
		if((p*t - q*s) % h != 0){
			cout<<-1<<endl;
			continue;
		}
		ll helik = (p*t-q*s)/h;
		x = x * helik;
		y = y * helik;
		ll h1 = p/h;
		ll h2 = (q-p)/h;
		if(x==0 && y==0){
			cout<<0<<endl;
			continue;
		}
		else if(x==0 && y>0){
			cout<<y<<endl;
			continue;
		}
		else if(y==0 && x>0){
			cout<<x<<endl;
			continue;
		}
		else if(h1==0 && h2==0){
			if(x>0 && y>0){
				cout<<(x+y)<<endl;
				continue;
			}
			else{
				cout<<-1<<endl;
				continue;
			}
		}
		else if(h1==0){
			if(x<0){
				cout<<-1<<endl;
				continue;
			}
			else if(y>0){
				ll sub = y/h2;
				y = y - sub*h2;
				cout<<(x+y)<<endl;
				continue;
			}
			else{
				ll sub = ceil((abs(y)*1.0)/(double(h2)));
				y = y + sub*h2;
				cout<<(x+y)<<endl;
				continue;
			}
		}
		else if(h2==0){
			if(y<0){
				cout<<-1<<endl;
				continue;
			}
			else if(x>0){
				ll sub = x/h1;
				x = x - sub*h1;
				cout<<(x+y)<<endl;
				continue;
			}
			else{
				ll sub = ceil((abs(x)*1.0)/(double(h1)));
				x = x + sub*h1;
				cout<<(x+y)<<endl;
				continue;
			}
		}
		else if(y<0 && h2==0){
			cout<<-1<<endl;
			continue;
		}
		else if(x>0 && y>0){
			h1 = abs(h1);
			h2 = abs(h2);
			ll t1 = x/h1;
			ll t2 = y/h2;
			ll sub = min(t1,t2);
			x = x - sub*h1;
			y = y - sub*h2;
			cout<<(x+y)<<endl;
			continue;
		}
		else if(x>0 && y<0){
			h1 = abs(h1);
			h2 = abs(h2);
			ll sub = ceil((abs(y)*1.0)/(double(h2)));
			x = x + sub*h1;
			y = y + sub*h2;
			cout<<(x+y)<<endl;
			continue;
		}
		else if(x<0 && y>0){
			h1 = abs(h1);
			h2 = abs(h2);
			ll sub = ceil((abs(x)*1.0)/(double(h1)));
			x = x + sub*h1;
			y = y + sub*h2;
			cout<<(x+y)<<endl;
			continue;
		}
		else{
			// cout<<x<<" "<<y<<" "<<h1<<" "<<h2<<endl;
			h1 = abs(h1);
			h2 = abs(h2);
			ll t1 = ceil((abs(x)*1.0)/(double(h1)));
			ll t2 = ceil((abs(y)*1.0)/(double(h2)));
			ll sub = max(t1,t2);
			// cout<<sub<<endl;
			x = x + sub*h1;
			y = y + sub*h2;
			cout<<(x+y)<<endl;
			continue;
		}
	}
	return 0;
}


/*input
4
3 10 1 2
7 14 3 8
20 70 2 7
5 6 1 1
*/
//IT TAKES EVERYTHING
#include <bits/stdc++.h>
using namespace std;

#define endl '\n'
#define fo(i,n) for(i=0;i<n;++i)
#define forr(i,n) for(i=n-1;i>=0;--i)

#define INF (int)1e9
#define EPS 1e-9

#define sz(x) (int)(x).size()
#define first fi
#define second se
#define mp make_pair
#define pb push_back
#define yolo "debug_statement"

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

int binary(int x, int y, int p, int q){
	int tlow = 0;
	int thigh = y; //taking it as 10^9 also works since y <= 10^9
	int tmid;
	int res = -1;
	while(tlow <= thigh){
		tmid = (tlow+thigh)>>1;

		if(1ll*p*tmid>=1ll*x && (1ll*q*tmid-1ll*p*tmid)>=1ll*(y-x)){
			res = tmid;
			thigh = tmid - 1;
		}
		else
			tlow = tmid + 1;
	}
	return res;
}

int main(){
	int t, x, y, p, q;
	scanf("%d",&t);
	while(t--){
		scanf("%d%d%d%d",&x,&y,&p,&q);
		int ans = binary(x,y,p,q);
		if(ans==-1){
			printf("-1\n");
			continue;
		}
		printf("%I64d\n",1ll*q*ans - y);
	}
	return 0;
}


/*input
4
3 10 1 2
7 14 3 8
20 70 2 7
5 6 1 1
*/
#include <bits/stdc++.h>
#define endl '\n'
#define fo(i,n) for(i=0;i<n;++i)
#define forr(i,n) for(i=n-1;i>=0;--i)
using namespace std;
typedef long long int ll;
typedef pair<int,int> ii;
typedef vector<ii> vii;
typedef vector<int> vi;

ll gcd (ll a, ll b, ll & x, ll & y) {
	if (a == 0) {
 x = 0; y = 1;
		return b;
	}
	ll x1, y1;
	ll d = gcd (b%a, a, x1, y1);
 x = y1 - (b / a) * x1;
 y = x1;
	return d;
}

int main(){
	ios_base::sync_with_stdio(0);
	cin.tie(NULL);
	cout.tie(NULL);
	int te;
	cin>>te;
	while(te--){
		ll s, t, p, q;
		cin>>s>>t>>p>>q;
		ll x, y;
		ll h = gcd(q-p,-p,x,y);
		if((p*t - q*s) % h != 0){
			cout<<-1<<endl;
			continue;
		}
		ll helik = (p*t-q*s)/h;
		x = x * helik;
		y = y * helik;
		ll h1 = p/h;
		ll h2 = (q-p)/h;
		if(x==0 && y==0){
			cout<<0<<endl;
			continue;
		}
		if(h1==0 && h2==0){
			if(x>0 && y>0){
				cout<<(x+y)<<endl;
				continue;
			}
			else{
				cout<<-1<<endl;
				continue;
			}
		}
		if(h1==0){
			if(x<0){
				cout<<-1<<endl;
				continue;
			}
			else if(y>0){
				ll sub = y/h2;
				y = y - sub*h2;
				cout<<(x+y)<<endl;
				continue;
			}
			else{
				ll sub = ceil((abs(y)*1.0)/(double(h2)));
				y = y + sub*h2;
				cout<<(x+y)<<endl;
				continue;
			}
		}
		else if(h2==0){
			if(y<0){
				cout<<-1<<endl;
				continue;
			}
			else if(x>0){
				ll sub = x/h1;
				x = x - sub*h1;
				cout<<(x+y)<<endl;
				continue;
			}
			else{
				ll sub = ceil((abs(x)*1.0)/(double(h1)));
				x = x + sub*h1;
				cout<<(x+y)<<endl;
				continue;
			}
		}
		if(x==0 && y<0){
			h1 = abs(h1);
			h2 = abs(h2);
			ll sub = ceil((abs(y)*1.0)/(double(h2)));
			x = x + sub*h1;
			y = y + sub*h2;
			cout<<(x+y)<<endl;
			continue;
		}
		if(x==0 && y>0){
			cout<<y<<endl;
			continue;
		}
		if(y==0 && x<0){
			h1 = abs(h1);
			h2 = abs(h2);
			ll sub = ceil((abs(x)*1.0)/(double(h1)));
			x = x + sub*h1;
			y = y + sub*h2;
			cout<<(x+y)<<endl;
			continue;
		}
		if(y==0 && x>0){
			cout<<x<<endl;
			continue;
		}
		if(y<0 && h2==0){
			cout<<-1<<endl;
			continue;
		}
		if(x>0 && y>0){
			h1 = abs(h1);
			h2 = abs(h2);
			ll t1 = x/h1;
			ll t2 = y/h2;
			ll sub = min(t1,t2);
			x = x - sub*h1;
			y = y - sub*h2;
			cout<<(x+y)<<endl;
			continue;
		}
		if(x>0 && y<0){
			h1 = abs(h1);
			h2 = abs(h2);
			ll sub = ceil((abs(y)*1.0)/(double(h2)));
			x = x + sub*h1;
			y = y + sub*h2;
			cout<<(x+y)<<endl;
			continue;
		}
		if(x<0 && y>0){
			h1 = abs(h1);
			h2 = abs(h2);
			ll sub = ceil((abs(x)*1.0)/(double(h1)));
			x = x + sub*h1;
			y = y + sub*h2;
			cout<<(x+y)<<endl;
			continue;
		}
		if(x>0 && y>0){
			// cout<<x<<" "<<y<<" "<<h1<<" "<<h2<<endl;
			h1 = abs(h1);
			h2 = abs(h2);
			ll t1 = ceil((abs(x)*1.0)/(double(h1)));
			ll t2 = ceil((abs(y)*1.0)/(double(h2)));
			ll sub = max(t1,t2);
			// cout<<sub<<endl;
			x = x + sub*h1;
			y = y + sub*h2;
			cout<<(x+y)<<endl;
			continue;
		}
	}
	return 0;
}


/*input
1
1 1 1 9
*/
#include <bits/stdc++.h>
#define endl '\n'
#define fo(i,n) for(i=0;i<n;++i)
#define forr(i,n) for(i=n-1;i>=0;--i)
using namespace std;
typedef long long int ll;
typedef pair<int,int> ii;
typedef vector<ii> vii;
typedef vector<int> vi;

ll gcd (ll a, ll b, ll & x, ll & y) {
	if (a == 0) {
 x = 0; y = 1;
		return b;
	}
	ll x1, y1;
	ll d = gcd (b%a, a, x1, y1);
 x = y1 - (b / a) * x1;
 y = x1;
	return d;
}

int main(){
	ios_base::sync_with_stdio(0);
	cin.tie(NULL);
	cout.tie(NULL);
	int te;
	cin>>te;
	while(te--){
		ll s, t, p, q;
		cin>>s>>t>>p>>q;
		ll x, y;
		ll h = gcd(q-p,-p,x,y);
		if((p*t - q*s) % h != 0){
			cout<<-1<<endl;
			return 0;
		}
		ll helik = (p*t-q*s)/h;
		x = x * helik;
		y = y * helik;
		ll h1 = p/h;
		ll h2 = (q-p)/h;
		// cout<<x<<" "<<y<<" "<<h1<<" "<<h2<<endl;
		if(x==0 && y==0){
			if(x<0 || y<0){
				cout<<-1<<endl;
				continue;
			}
			cout<<0<<endl;
			continue;
		}
		else if(x==0 && y>0){
			if(x<0 || y<0){
				cout<<-1<<endl;
				continue;
			}
			cout<<y<<endl;
			continue;
		}
		else if(y==0 && x>0){
			if(x<0 || y<0){
				cout<<-1<<endl;
				continue;
			}
			cout<<x<<endl;
			continue;
		}
		else if(h1==0 && h2==0){
			if(x>0 && y>0){
				if(x<0 || y<0){
					cout<<-1<<endl;
					continue;
				}
				cout<<(x+y)<<endl;
				continue;
			}
			else{
				if(x<0 || y<0){
					cout<<-1<<endl;
					continue;
				}
				cout<<-1<<endl;
				continue;
			}
		}
		else if(h1==0){
			if(x<0){
				if(x<0 || y<0){
					cout<<-1<<endl;
					continue;
				}
				cout<<-1<<endl;
				continue;
			}
			else if(y>0){
				if(x<0 || y<0){
					cout<<-1<<endl;
					continue;
				}
				cout<<(x+y)<<endl;
				continue;
			}
			else{
				ll sub = ceil((abs(y)*1.0)/(double(h2)));
				y = y + sub*h2;
				if(x<0 || y<0){
					cout<<-1<<endl;
					continue;
				}
				cout<<(x+y)<<endl;
				continue;
			}
		}
		else if(h2==0){
			if(y<0){
				if(x<0 || y<0){
					cout<<-1<<endl;
					continue;
				}
				cout<<-1<<endl;
				continue;
			}
			else if(x>0){
				if(x<0 || y<0){
					cout<<-1<<endl;
					continue;
				}
				cout<<(x+y)<<endl;
				continue;
			}
			else{
				ll sub = ceil((abs(x)*1.0)/(double(h1)));
				x = x + sub*h1;
				if(x<0 || y<0){
					cout<<-1<<endl;
					continue;
				}
				cout<<(x+y)<<endl;
				continue;
			}
		}
		else if(y<0 && h2==0){
			cout<<-1<<endl;
			continue;
		}
		else if(x>0 && y>0){
			h1 = abs(h1);
			h2 = abs(h2);
			ll t1 = x/h1;
			ll t2 = y/h2;
			ll sub = min(t1,t2);
			x = x - sub*h1;
			y = y - sub*h2;
			if(x<0 || y<0){
				cout<<-1<<endl;
				continue;
			}
			cout<<(x+y)<<endl;
			continue;
		}
		else if(x>0 && y<0){
			h1 = abs(h1);
			h2 = abs(h2);
			ll sub = ceil((abs(y)*1.0)/(double(h2)));
			x = x + sub*h1;
			y = y + sub*h2;
			if(x<0 || y<0){
				cout<<-1<<endl;
				continue;
			}
			cout<<(x+y)<<endl;
			continue;
		}
		else if(x<0 && y>0){
			h1 = abs(h1);
			h2 = abs(h2);
			ll sub = ceil((abs(x)*1.0)/(double(h1)));
			x = x + sub*h1;
			y = y + sub*h2;
			if(x<0 || y<0){
				cout<<-1<<endl;
				continue;
			}
			cout<<(x+y)<<endl;
			continue;
		}
		else{
			// cout<<x<<" "<<y<<" "<<h1<<" "<<h2<<endl;
			h1 = abs(h1);
			h2 = abs(h2);
			ll t1 = ceil((abs(x)*1.0)/(double(h1)));
			ll t2 = ceil((abs(y)*1.0)/(double(h2)));
			ll sub = max(t1,t2);
			// cout<<sub<<endl;
			x = x + sub*h1;
			y = y + sub*h2;
			if(x<0 || y<0){
				cout<<-1<<endl;
				continue;
			}
			cout<<(x+y)<<endl;
			continue;
		}
	}
	return 0;
}


/*input
5
1 1 1 2
3 10 1 2
7 14 3 8
20 70 2 7
5 6 1 1
*/
#include <bits/stdc++.h>
#define endl '\n'
#define fo(i,n) for(i=0;i<n;++i)
#define forr(i,n) for(i=n-1;i>=0;--i)
using namespace std;
typedef long long int ll;
typedef pair<int,int> ii;
typedef vector<ii> vii;
typedef vector<int> vi;

int gcd (int a, int b, int & x, int & y) {
	if (a == 0) {
 x = 0; y = 1;
		return b;
	}
	int x1, y1;
	int d = gcd (b%a, a, x1, y1);
 x = y1 - (b / a) * x1;
 y = x1;
	return d;
}

int main(){
	ios_base::sync_with_stdio(0);
	cin.tie(NULL);
	cout.tie(NULL);
	int te;
	cin>>te;
	while(te--){
		int s, t, p, q;
		cin>>s>>t>>p>>q;
		int x, y;
		int h = gcd(q-p,-p,x,y);
		if((p*t - q*s) % h != 0){
			cout<<-1<<endl;
			return 0;
		}
		int helik = (p*t-q*s)/h;
		x = x * helik;
		y = y * helik;
		int h1 = p/h;
		int h2 = (q-p)/h;
		// cout<<x<<" "<<y<<" "<<h1<<" "<<h2<<endl;
		if(x==0 && y==0){
			cout<<0<<endl;
			continue;
		}
		else if(x==0 && y>0){
			cout<<y<<endl;
			continue;
		}
		else if(y==0 && x>0){
			cout<<x<<endl;
			continue;
		}
		if(x<0 && h1==0){
			cout<<-1<<endl;
			continue;
		}
		else if(y<0 && h2==0){
			cout<<-1<<endl;
			continue;
		}
		else if(x>0 && y>0){
			h1 = abs(h1);
			h2 = abs(h2);
			int t1 = x/h1;
			int t2 = y/h2;
			int sub = min(t1,t2);
			x = x - sub*h1;
			y = y - sub*h2;
			cout<<(x+y)<<endl;
			continue;
		}
		else if(x>0 && y<0){
			h1 = abs(h1);
			h2 = abs(h2);
			int sub = ceil((abs(y)*1.0)/(double(h2)));
			x = x + sub*h1;
			y = y + sub*h2;
			cout<<(x+y)<<endl;
			continue;
		}
		else if(x<0 && y>0){
			h1 = abs(h1);
			h2 = abs(h2);
			int sub = ceil((abs(x)*1.0)/(double(h1)));
			x = x + sub*h1;
			y = y + sub*h2;
			cout<<(x+y)<<endl;
			continue;
		}
		else{
			// cout<<x<<" "<<y<<" "<<h1<<" "<<h2<<endl;
			h1 = abs(h1);
			h2 = abs(h2);
			int t1 = ceil((abs(x)*1.0)/(double(h1)));
			int t2 = ceil((abs(y)*1.0)/(double(h2)));
			int sub = max(t1,t2);
			x = x + sub*h1;
			y = y + sub*h2;
			cout<<(x+y)<<endl;
			continue;
		}
	}
	return 0;
}


/*input
5
1 1 1 1
3 10 1 2
7 14 3 8
20 70 2 7
5 6 1 1
*/
#include <bits/stdc++.h>
#define endl '\n'
#define fo(i,n) for(i=0;i<n;++i)
#define forr(i,n) for(i=n-1;i>=0;--i)
using namespace std;
typedef long long int ll;
typedef pair<int,int> ii;
typedef vector<ii> vii;
typedef vector<int> vi;

int gcd (int a, int b, int & x, int & y) {
	if (a == 0) {
 x = 0; y = 1;
		return b;
	}
	int x1, y1;
	int d = gcd (b%a, a, x1, y1);
 x = y1 - (b / a) * x1;
 y = x1;
	return d;
}

int main(){
	ios_base::sync_with_stdio(0);
	cin.tie(NULL);
	cout.tie(NULL);
	int te;
	cin>>te;
	while(te--){
		int s, t, p, q;
		cin>>s>>t>>p>>q;
		int x, y;
		int h = gcd(q-p,-p,x,y);
		if((p*t - q*s) % h != 0){
			cout<<-1<<endl;
			return 0;
		}
		int helik = (p*t-q*s)/h;
		x = x * helik;
		y = y * helik;
		int h1 = p/h;
		int h2 = (q-p)/h;
		// cout<<x<<" "<<y<<" "<<h1<<" "<<h2<<endl;
		if(x==0 && y==0){
			cout<<0<<endl;
			continue;
		}
		else if(x==0 && y>0){
			cout<<y<<endl;
			continue;
		}
		else if(y==0 && x>0){
			cout<<x<<endl;
			continue;
		}
		if(x<0 && h1==0){
			cout<<-1<<endl;
			continue;
		}
		else if(y<0 && h2==0){
			cout<<-1<<endl;
			continue;
		}
		else if(x>0 && y>0){
			h1 = abs(h1);
			h2 = abs(h2);
			int t1 = x/h1;
			int t2 = y/h2;
			int sub = min(t1,t2);
			x = x - sub*h1;
			y = y - sub*h2;
			cout<<(x+y)<<endl;
			continue;
		}
		else if(x>0 && y<0){
			h1 = abs(h1);
			h2 = abs(h2);
			int sub = abs(y)/h2;
			sub++;
			x = x + sub*h1;
			y = y + sub*h2;
			cout<<(x+y)<<endl;
			continue;
		}
		else if(x<0 && y>0){
			h1 = abs(h1);
			h2 = abs(h2);
			int sub = abs(x)/h1;
			sub++;
			x = x + sub*h1;
			y = y + sub*h2;
			cout<<(x+y)<<endl;
			continue;
		}
		else{
			h1 = abs(h1);
			h2 = abs(h2);
			int t1 = abs(x)/h1;
			int t2 = abs(y)/h2;
			int sub = max(t1,t2);
			sub++;
			x = x + sub*h1;
			y = y + sub*h2;
			cout<<(x+y)<<endl;
			continue;
		}
	}
	return 0;
}


/*input
4
3 10 1 2
7 14 3 8
20 70 2 7
5 6 1 1
*/
#include <bits/stdc++.h>
#define endl '\n'
#define fo(i,n) for(i=0;i<n;++i)
#define forr(i,n) for(i=n-1;i>=0;--i)
using namespace std;
typedef long long int ll;
typedef pair<int,int> ii;
typedef vector<ii> vii;
typedef vector<int> vi;

ll gcd (ll a, ll b, ll & x, ll & y) {
	if (a == 0) {
 x = 0; y = 1;
		return b;
	}
	ll x1, y1;
	ll d = gcd (b%a, a, x1, y1);
 x = y1 - (b / a) * x1;
 y = x1;
	return d;
}

int main(){
	ios_base::sync_with_stdio(0);
	cin.tie(NULL);
	cout.tie(NULL);
	int te;
	cin>>te;
	while(te--){
		ll s, t, p, q;
		cin>>s>>t>>p>>q;
		ll x, y;
		ll h = gcd(q-p,-p,x,y);
		if((p*t - q*s) % h != 0){
			cout<<-1<<endl;
			continue;
		}
		ll helik = (p*t-q*s)/h;
		x = x * helik;
		y = y * helik;
		ll h1 = p/h;
		ll h2 = (q-p)/h;
		if(x==0 && y==0){
			cout<<0<<endl;
			continue;
		}
		if(h1==0 && h2==0){
			if(x>=0 && y>=0){
				cout<<(x+y)<<endl;
				continue;
			}
			else{
				cout<<-1<<endl;
				continue;
			}
		}
		if(h1==0){
			if(x<0){
				cout<<-1<<endl;
				continue;
			}
			else if(y>=0){
				ll sub = y/h2;
				y = y - sub*h2;
				cout<<(x+y)<<endl;
				continue;
			}
			else{
				ll sub = ceil((abs(y)*1.0)/(double(h2)));
				y = y + sub*h2;
				cout<<(x+y)<<endl;
				continue;
			}
		}
		else if(h2==0){
			if(y<0){
				cout<<-1<<endl;
				continue;
			}
			else if(x>=0){
				ll sub = x/h1;
				x = x - sub*h1;
				cout<<(x+y)<<endl;
				continue;
			}
			else{
				ll sub = ceil((abs(x)*1.0)/(double(h1)));
				x = x + sub*h1;
				cout<<(x+y)<<endl;
				continue;
			}
		}

		if(x==0 && y>=0){
			cout<<y<<endl;
			continue;
		}

		if(y==0 && x>=0){
			cout<<x<<endl;
			continue;
		}
		if(y<0 && h2==0){
			cout<<-1<<endl;
			continue;
		}
		if(x>=0 && y>=0){
			h1 = abs(h1);
			h2 = abs(h2);
			ll t1 = x/h1;
			ll t2 = y/h2;
			ll sub = min(t1,t2);
			x = x - sub*h1;
			y = y - sub*h2;
			cout<<(x+y)<<endl;
			continue;
		}
		if(x>=0 && y<0){
			h1 = abs(h1);
			h2 = abs(h2);
			ll sub = ceil((abs(y)*1.0)/(double(h2)));
			x = x + sub*h1;
			y = y + sub*h2;
			cout<<(x+y)<<endl;
			continue;
		}
		if(x<0 && y>=0){
			h1 = abs(h1);
			h2 = abs(h2);
			ll sub = ceil((abs(x)*1.0)/(double(h1)));
			x = x + sub*h1;
			y = y + sub*h2;
			cout<<(x+y)<<endl;
			continue;
		}
		if(x<0 && y<0){
			// cout<<x<<" "<<y<<" "<<h1<<" "<<h2<<endl;
			h1 = abs(h1);
			h2 = abs(h2);
			ll t1 = ceil((abs(x)*1.0)/(double(h1)));
			ll t2 = ceil((abs(y)*1.0)/(double(h2)));
			ll sub = max(t1,t2);
			// cout<<sub<<endl;
			x = x + sub*h1;
			y = y + sub*h2;
			cout<<(x+y)<<endl;
			continue;
		}
	}
	return 0;
}


/*input
4
3 10 1 2
7 14 3 8
20 70 2 7
5 6 1 1
*/
#include <bits/stdc++.h>
#define endl '\n'
#define fo(i,n) for(i=0;i<n;++i)
#define forr(i,n) for(i=n-1;i>=0;--i)
using namespace std;
typedef long long int ll;
typedef pair<int,int> ii;
typedef vector<ii> vii;
typedef vector<int> vi;

ll gcd (ll a, ll b, ll & x, ll & y) {
	if (a == 0) {
 x = 0; y = 1;
		return b;
	}
	ll x1, y1;
	ll d = gcd (b%a, a, x1, y1);
 x = y1 - (b / a) * x1;
 y = x1;
	return d;
}

int main(){
	ios_base::sync_with_stdio(0);
	cin.tie(NULL);
	cout.tie(NULL);
	int te;
	cin>>te;
	while(te--){
		ll s, t, p, q;
		cin>>s>>t>>p>>q;
		ll x, y;
		ll h = gcd(q-p,-p,x,y);
		if((p*t - q*s) % h != 0){
			cout<<-1<<endl;
			continue;
		}
		ll helik = (p*t-q*s)/h;
		x = x * helik;
		y = y * helik;
		ll h1 = p/h;
		ll h2 = (q-p)/h;
		if(x==0 && y==0){
			cout<<0<<endl;
			continue;
		}
		if(h1==0 && h2==0){
			if(x>0 && y>0){
				cout<<(x+y)<<endl;
				continue;
			}
			else{
				cout<<-1<<endl;
				continue;
			}
		}
		if(h1==0){
			if(x<0){
				cout<<-1<<endl;
				continue;
			}
			else if(y>0){
				ll sub = y/h2;
				y = y - sub*h2;
				cout<<(x+y)<<endl;
				continue;
			}
			else{
				ll sub = ceil((abs(y)*1.0)/(double(h2)));
				y = y + sub*h2;
				cout<<(x+y)<<endl;
				continue;
			}
		}
		else if(h2==0){
			if(y<0){
				cout<<-1<<endl;
				continue;
			}
			else if(x>0){
				ll sub = x/h1;
				x = x - sub*h1;
				cout<<(x+y)<<endl;
				continue;
			}
			else{
				ll sub = ceil((abs(x)*1.0)/(double(h1)));
				x = x + sub*h1;
				cout<<(x+y)<<endl;
				continue;
			}
		}
		if(x==0 && y<0){
			h1 = abs(h1);
			h2 = abs(h2);
			ll sub = ceil((abs(y)*1.0)/(double(h2)));
			x = x + sub*h1;
			y = y + sub*h2;
			cout<<(x+y)<<endl;
			continue;
		}
		if(x==0 && y>0){
			cout<<y<<endl;
			continue;
		}
		if(y==0 && x<0){
			h1 = abs(h1);
			h2 = abs(h2);
			ll sub = ceil((abs(x)*1.0)/(double(h1)));
			x = x + sub*h1;
			y = y + sub*h2;
			cout<<(x+y)<<endl;
			continue;
		}
		if(y==0 && x>0){
			cout<<x<<endl;
			continue;
		}
		if(y<0 && h2==0){
			cout<<-1<<endl;
			continue;
		}
		if(x>0 && y>0){
			h1 = abs(h1);
			h2 = abs(h2);
			ll t1 = x/h1;
			ll t2 = y/h2;
			ll sub = min(t1,t2);
			x = x - sub*h1;
			y = y - sub*h2;
			cout<<(x+y)<<endl;
			continue;
		}
		if(x>0 && y<0){
			h1 = abs(h1);
			h2 = abs(h2);
			ll sub = ceil((abs(y)*1.0)/(double(h2)));
			x = x + sub*h1;
			y = y + sub*h2;
			cout<<(x+y)<<endl;
			continue;
		}
		if(x<0 && y>0){
			h1 = abs(h1);
			h2 = abs(h2);
			ll sub = ceil((abs(x)*1.0)/(double(h1)));
			x = x + sub*h1;
			y = y + sub*h2;
			cout<<(x+y)<<endl;
			continue;
		}
		else{
			// cout<<x<<" "<<y<<" "<<h1<<" "<<h2<<endl;
			h1 = abs(h1);
			h2 = abs(h2);
			ll t1 = ceil((abs(x)*1.0)/(double(h1)));
			ll t2 = ceil((abs(y)*1.0)/(double(h2)));
			ll sub = max(t1,t2);
			// cout<<sub<<endl;
			x = x + sub*h1;
			y = y + sub*h2;
			cout<<(x+y)<<endl;
			continue;
		}
	}
	return 0;
}


/*input
4
3 10 1 2
7 14 3 8
20 70 2 7
5 6 1 1
*/
#include <bits/stdc++.h>
#define endl '\n'
#define fo(i,n) for(i=0;i<n;++i)
#define forr(i,n) for(i=n-1;i>=0;--i)
using namespace std;
typedef long long int ll;
typedef pair<int,int> ii;
typedef vector<ii> vii;
typedef vector<int> vi;

ll gcd (ll a, ll b, ll & x, ll & y) {
	if (a == 0) {
 x = 0; y = 1;
		return b;
	}
	ll x1, y1;
	ll d = gcd (b%a, a, x1, y1);
 x = y1 - (b / a) * x1;
 y = x1;
	return d;
}

int main(){
	ios_base::sync_with_stdio(0);
	cin.tie(NULL);
	cout.tie(NULL);
	int te;
	cin>>te;
	while(te--){
		ll s, t, p, q;
		cin>>s>>t>>p>>q;
		ll x, y;
		ll h = gcd(q-p,-p,x,y);
		if((p*t - q*s) % h != 0){
			cout<<-1<<endl;
			continue;
		}
		ll helik = (p*t-q*s)/h;
		x = x * helik;
		y = y * helik;
		ll h1 = p/h;
		ll h2 = (q-p)/h;
		if(x==0 && y==0){
			cout<<0<<endl;
			continue;
		}
		if(x==0 && y>0){
			cout<<y<<endl;
			continue;
		}
		if(y==0 && x>0){
			cout<<x<<endl;
			continue;
		}
		if(h1==0 && h2==0){
			if(x>0 && y>0){
				cout<<(x+y)<<endl;
				continue;
			}
			else{
				cout<<-1<<endl;
				continue;
			}
		}
		if(h1==0){
			if(x<0){
				cout<<-1<<endl;
				continue;
			}
			else if(y>0){
				ll sub = y/h2;
				y = y - sub*h2;
				cout<<(x+y)<<endl;
				continue;
			}
			else{
				ll sub = ceil((abs(y)*1.0)/(double(h2)));
				y = y + sub*h2;
				cout<<(x+y)<<endl;
				continue;
			}
		}
		else if(h2==0){
			if(y<0){
				cout<<-1<<endl;
				continue;
			}
			else if(x>0){
				ll sub = x/h1;
				x = x - sub*h1;
				cout<<(x+y)<<endl;
				continue;
			}
			else{
				ll sub = ceil((abs(x)*1.0)/(double(h1)));
				x = x + sub*h1;
				cout<<(x+y)<<endl;
				continue;
			}
		}
		if(y<0 && h2==0){
			cout<<-1<<endl;
			continue;
		}
		if(x>0 && y>0){
			h1 = abs(h1);
			h2 = abs(h2);
			ll t1 = x/h1;
			ll t2 = y/h2;
			ll sub = min(t1,t2);
			x = x - sub*h1;
			y = y - sub*h2;
			cout<<(x+y)<<endl;
			continue;
		}
		if(x>0 && y<0){
			h1 = abs(h1);
			h2 = abs(h2);
			ll sub = ceil((abs(y)*1.0)/(double(h2)));
			x = x + sub*h1;
			y = y + sub*h2;
			cout<<(x+y)<<endl;
			continue;
		}
		if(x<0 && y>0){
			h1 = abs(h1);
			h2 = abs(h2);
			ll sub = ceil((abs(x)*1.0)/(double(h1)));
			x = x + sub*h1;
			y = y + sub*h2;
			cout<<(x+y)<<endl;
			continue;
		}
		else{
			// cout<<x<<" "<<y<<" "<<h1<<" "<<h2<<endl;
			h1 = abs(h1);
			h2 = abs(h2);
			ll t1 = ceil((abs(x)*1.0)/(double(h1)));
			ll t2 = ceil((abs(y)*1.0)/(double(h2)));
			ll sub = max(t1,t2);
			// cout<<sub<<endl;
			x = x + sub*h1;
			y = y + sub*h2;
			cout<<(x+y)<<endl;
			continue;
		}
	}
	return 0;
}


/*input
4
3 10 1 2
7 14 3 8
20 70 2 7
5 6 1 1
*/
#include <bits/stdc++.h>
#define endl '\n'
#define fo(i,n) for(i=0;i<n;++i)
#define forr(i,n) for(i=n-1;i>=0;--i)
using namespace std;
typedef long long int ll;
typedef pair<int,int> ii;
typedef vector<ii> vii;
typedef vector<int> vi;

ll gcd (ll a, ll b, ll & x, ll & y) {
	if (a == 0) {
 x = 0; y = 1;
		return b;
	}
	ll x1, y1;
	ll d = gcd (b%a, a, x1, y1);
 x = y1 - (b / a) * x1;
 y = x1;
	return d;
}

int main(){
	ios_base::sync_with_stdio(0);
	cin.tie(NULL);
	cout.tie(NULL);
	int te;
	cin>>te;
	while(te--){
		ll s, t, p, q;
		cin>>s>>t>>p>>q;
		ll x, y;
		ll h = gcd(q-p,-p,x,y);
		if((p*t - q*s) % h != 0){
			cout<<-1<<endl;
			continue;
		}
		ll helik = (p*t-q*s)/h;
		x = x * helik;
		y = y * helik;
		ll h1 = p/h;
		ll h2 = (q-p)/h;
		if(x==0 && y==0){
			cout<<0<<endl;
			continue;
		}
		if(h1==0 && h2==0){
			if(x>0 && y>0){
				cout<<(x+y)<<endl;
				continue;
			}
			else{
				cout<<-1<<endl;
				continue;
			}
		}
		if(h1==0){
			if(x<0){
				cout<<-1<<endl;
				continue;
			}
			else if(y>0){
				ll sub = y/h2;
				y = y - sub*h2;
				cout<<(x+y)<<endl;
				continue;
			}
			else{
				ll sub = ceil((abs(y)*1.0)/(double(h2)));
				y = y + sub*h2;
				cout<<(x+y)<<endl;
				continue;
			}
		}
		else if(h2==0){
			if(y<0){
				cout<<-1<<endl;
				continue;
			}
			else if(x>0){
				ll sub = x/h1;
				x = x - sub*h1;
				cout<<(x+y)<<endl;
				continue;
			}
			else{
				ll sub = ceil((abs(x)*1.0)/(double(h1)));
				x = x + sub*h1;
				cout<<(x+y)<<endl;
				continue;
			}
		}
		// if(x==0 && y<0){
		// 	h1 = abs(h1);
		// 	h2 = abs(h2);
		// 	ll sub = ceil((abs(y)*1.0)/(double(h2)));
		// 	x = x + sub*h1;
		// 	y = y + sub*h2;
		// 	cout<<(x+y)<<endl;
		// 	continue;
		// }
		if(x==0 && y>0){
			cout<<y<<endl;
			continue;
		}
		if(y==0 && x<0){
			h1 = abs(h1);
			h2 = abs(h2);
			ll sub = ceil((abs(x)*1.0)/(double(h1)));
			x = x + sub*h1;
			y = y + sub*h2;
			cout<<(x+y)<<endl;
			continue;
		}
		if(y==0 && x>0){
			cout<<x<<endl;
			continue;
		}
		if(y<0 && h2==0){
			cout<<-1<<endl;
			continue;
		}
		if(x>0 && y>0){
			h1 = abs(h1);
			h2 = abs(h2);
			ll t1 = x/h1;
			ll t2 = y/h2;
			ll sub = min(t1,t2);
			x = x - sub*h1;
			y = y - sub*h2;
			cout<<(x+y)<<endl;
			continue;
		}
		if(x>0 && y<0){
			h1 = abs(h1);
			h2 = abs(h2);
			ll sub = ceil((abs(y)*1.0)/(double(h2)));
			x = x + sub*h1;
			y = y + sub*h2;
			cout<<(x+y)<<endl;
			continue;
		}
		if(x<0 && y>0){
			h1 = abs(h1);
			h2 = abs(h2);
			ll sub = ceil((abs(x)*1.0)/(double(h1)));
			x = x + sub*h1;
			y = y + sub*h2;
			cout<<(x+y)<<endl;
			continue;
		}
		if(x>0 && y>0){
			// cout<<x<<" "<<y<<" "<<h1<<" "<<h2<<endl;
			h1 = abs(h1);
			h2 = abs(h2);
			ll t1 = ceil((abs(x)*1.0)/(double(h1)));
			ll t2 = ceil((abs(y)*1.0)/(double(h2)));
			ll sub = max(t1,t2);
			// cout<<sub<<endl;
			x = x + sub*h1;
			y = y + sub*h2;
			cout<<(x+y)<<endl;
			continue;
		}
	}
	return 0;
}


/*input
4
3 10 1 2
7 14 3 8
20 70 2 7
5 6 1 1
*/
#include <bits/stdc++.h>
#define endl '\n'
#define fo(i,n) for(i=0;i<n;++i)
#define forr(i,n) for(i=n-1;i>=0;--i)
using namespace std;
typedef long long int ll;
typedef pair<int,int> ii;
typedef vector<ii> vii;
typedef vector<int> vi;

ll gcd (ll a, ll b, ll & x, ll & y) {
	if (a == 0) {
 x = 0; y = 1;
		return b;
	}
	ll x1, y1;
	ll d = gcd (b%a, a, x1, y1);
 x = y1 - (b / a) * x1;
 y = x1;
	return d;
}

int main(){
	ios_base::sync_with_stdio(0);
	cin.tie(NULL);
	cout.tie(NULL);
	int te;
	cin>>te;
	while(te--){
		ll s, t, p, q;
		cin>>s>>t>>p>>q;
		ll x, y;
		ll h = gcd(q-p,-p,x,y);
		// if((p*t - q*s) % h != 0){
		// 	cout<<-1<<endl;
		// 	continue;
		// }
		ll helik = (p*t-q*s)/h;
		x = x * helik;
		y = y * helik;
		ll h1 = p/h;
		ll h2 = (q-p)/h;
		if(x==0 && y==0){
			cout<<0<<endl;
			continue;
		}
		if(h1==0 && h2==0){
			if(x>=0 && y>=0){
				cout<<(x+y)<<endl;
				continue;
			}
			else{
				cout<<-1<<endl;
				continue;
			}
		}
		if(h1==0){
			if(x<0){
				cout<<-1<<endl;
				continue;
			}
			else if(y>=0){
				ll sub = y/h2;
				y = y - sub*h2;
				cout<<(x+y)<<endl;
				continue;
			}
			else{
				ll sub = ceil((abs(y)*1.0)/(double(h2)));
				y = y + sub*h2;
				cout<<(x+y)<<endl;
				continue;
			}
		}
		else if(h2==0){
			if(y<0){
				cout<<-1<<endl;
				continue;
			}
			else if(x>=0){
				ll sub = x/h1;
				x = x - sub*h1;
				cout<<(x+y)<<endl;
				continue;
			}
			else{
				ll sub = ceil((abs(x)*1.0)/(double(h1)));
				x = x + sub*h1;
				cout<<(x+y)<<endl;
				continue;
			}
		}

		if(x==0 && y>=0){
			cout<<y<<endl;
			continue;
		}

		if(y==0 && x>=0){
			cout<<x<<endl;
			continue;
		}
		if(y<0 && h2==0){
			cout<<-1<<endl;
			continue;
		}
		if(x>=0 && y>=0){
			h1 = abs(h1);
			h2 = abs(h2);
			ll t1 = x/h1;
			ll t2 = y/h2;
			ll sub = min(t1,t2);
			x = x - sub*h1;
			y = y - sub*h2;
			cout<<(x+y)<<endl;
			continue;
		}
		if(x>=0 && y<0){
			h1 = abs(h1);
			h2 = abs(h2);
			ll sub = ceil((abs(y)*1.0)/(double(h2)));
			x = x + sub*h1;
			y = y + sub*h2;
			cout<<(x+y)<<endl;
			continue;
		}
		if(x<0 && y>=0){
			h1 = abs(h1);
			h2 = abs(h2);
			ll sub = ceil((abs(x)*1.0)/(double(h1)));
			x = x + sub*h1;
			y = y + sub*h2;
			cout<<(x+y)<<endl;
			continue;
		}
		if(x<0 && y<0){
			// cout<<x<<" "<<y<<" "<<h1<<" "<<h2<<endl;
			h1 = abs(h1);
			h2 = abs(h2);
			ll t1 = ceil((abs(x)*1.0)/(double(h1)));
			ll t2 = ceil((abs(y)*1.0)/(double(h2)));
			ll sub = max(t1,t2);
			// cout<<sub<<endl;
			x = x + sub*h1;
			y = y + sub*h2;
			cout<<(x+y)<<endl;
			continue;
		}
	}
	return 0;
}


/*input
5
1 1 7 8
3 10 1 2
7 14 3 8
20 70 2 7
5 6 1 1
*/
#include <bits/stdc++.h>
#define endl '\n'
#define fo(i,n) for(i=0;i<n;++i)
#define forr(i,n) for(i=n-1;i>=0;--i)
using namespace std;
typedef long long int ll;
typedef pair<int,int> ii;
typedef vector<ii> vii;
typedef vector<int> vi;

ll gcd (ll a, ll b, ll & x, ll & y) {
	if (a == 0) {
 x = 0; y = 1;
		return b;
	}
	ll x1, y1;
	ll d = gcd (b%a, a, x1, y1);
 x = y1 - (b / a) * x1;
 y = x1;
	return d;
}

int main(){
	ios_base::sync_with_stdio(0);
	cin.tie(NULL);
	cout.tie(NULL);
	int te;
	cin>>te;
	while(te--){
		ll s, t, p, q;
		cin>>s>>t>>p>>q;
		ll x, y;
		ll h = gcd(q-p,-p,x,y);
		if((p*t - q*s) % h != 0){
			cout<<-1<<endl;
			return 0;
		}
		ll helik = (p*t-q*s)/h;
		x = x * helik;
		y = y * helik;
		ll h1 = p/h;
		ll h2 = (q-p)/h;
		// cout<<x<<" "<<y<<" "<<h1<<" "<<h2<<endl;
		if(x==0 && y==0){
			cout<<0<<endl;
			continue;
		}
		else if(x==0 && y>0){
			cout<<y<<endl;
			continue;
		}
		else if(y==0 && x>0){
			cout<<x<<endl;
			continue;
		}
		else if(h1==0 && h2==0){
			if(x>0 && y>0){
				cout<<(x+y)<<endl;
				continue;
			}
			else{
				cout<<-1<<endl;
				continue;
			}
		}
		else if(h1==0){
			if(x<0){
				cout<<-1<<endl;
				continue;
			}
			else if(y>0){
				cout<<(x+y)<<endl;
				continue;
			}
			else{
				ll sub = ceil((abs(y)*1.0)/(double(h2)));
				y = y + sub*h2;
				cout<<(x+y)<<endl;
				continue;
			}
		}
		else if(h2==0){
			if(y<0){
				cout<<-1<<endl;
				continue;
			}
			else if(x>0){
				cout<<(x+y)<<endl;
				continue;
			}
			else{
				ll sub = ceil((abs(x)*1.0)/(double(h1)));
				x = x + sub*h1;
				cout<<(x+y)<<endl;
				continue;
			}
		}
		else if(y<0 && h2==0){
			cout<<-1<<endl;
			continue;
		}
		else if(x>0 && y>0){
			h1 = abs(h1);
			h2 = abs(h2);
			ll t1 = x/h1;
			ll t2 = y/h2;
			ll sub = min(t1,t2);
			x = x - sub*h1;
			y = y - sub*h2;
			cout<<(x+y)<<endl;
			continue;
		}
		else if(x>0 && y<0){
			h1 = abs(h1);
			h2 = abs(h2);
			ll sub = ceil((abs(y)*1.0)/(double(h2)));
			x = x + sub*h1;
			y = y + sub*h2;
			cout<<(x+y)<<endl;
			continue;
		}
		else if(x<0 && y>0){
			h1 = abs(h1);
			h2 = abs(h2);
			ll sub = ceil((abs(x)*1.0)/(double(h1)));
			x = x + sub*h1;
			y = y + sub*h2;
			cout<<(x+y)<<endl;
			continue;
		}
		else{
			// cout<<x<<" "<<y<<" "<<h1<<" "<<h2<<endl;
			h1 = abs(h1);
			h2 = abs(h2);
			ll t1 = ceil((abs(x)*1.0)/(double(h1)));
			ll t2 = ceil((abs(y)*1.0)/(double(h2)));
			ll sub = max(t1,t2);
			// cout<<sub<<endl;
			x = x + sub*h1;
			y = y + sub*h2;
			cout<<(x+y)<<endl;
			continue;
		}
	}
	return 0;
}


#include <bits/stdc++.h>
#define NOTFOUND -1

using namespace std;
int x, y, p, q, t;

int solv() {
	long long rng = 10000000;
	long long ans = 1;
	long long some = 1000000;
	long long mp = p;
	long long mq = q;
	long long res = NOTFOUND;
	bool b = false;
	int oldres = -1;
	int last = 0;
	long long t = 10000;
	while (t--) {
		if (mp - x <= mq - y && mp >= x && mq >= y) {
			if (last == 2)
				rng /= 2;
			oldres = res;
			if (res != -1)
				res = min(res, mq - y);
			else
				res = mq - y;
			rng = min(rng, ans - 1);
			ans -= rng;
			last = 1;
		}
		else {
			if (last == 1)
				rng /= 2;
			ans += rng / 2;
			last = 2;
		}
		mp = p * ans;
		mq = q * ans;
	}
	return res;
}
int main() {
	/*freopen("C.in", "r", stdin);
	freopen("C.out", "w", stdout);*/
	cin >> t;
	while (t--) {
		cin >> x >> y >> p >> q;
		long long res = solv();
		if (res == NOTFOUND) {
			cout << -1 << endl;
		}
		else
			cout << res << endl;
	}
	return 0;
}


#include <bits/stdc++.h>
#define NOTFOUND -1

using namespace std;
int x, y, p, q, t;

int solv() {
	long long rng = 10000000;
	long long ans = 1;
	long long some = 1000000;
	long long mp = p;
	long long mq = q;
	long long res = NOTFOUND;
	bool b = false;
	int oldres = -1;
	int last = 0;
	long long t = 10000;
	while (t--) {
		if (mp - x <= mq - y && mp >= x && mq >= y && mp > mq) {
			if (last == 2)
				rng /= 2;
			oldres = res;
			if (res != -1)
				res = min(res, mq - y);
			else
				res = mq - y;
			rng = min(rng, ans - 1);
			ans -= rng;
			last = 1;
		}
		else {
			if (last == 1)
				rng /= 2;
			ans += rng / 2;
			last = 2;
		}
		mp = p * ans;
		mq = q * ans;
	}
	return res;
}
int main() {
	/*freopen("C.in", "r", stdin);
	freopen("C.out", "w", stdout);*/
	cin >> t;
	while (t--) {
		cin >> x >> y >> p >> q;
		long long res = solv();
		if (res == NOTFOUND) {
			cout << -1 << endl;
		}
		else
			cout << res << endl;
	}
	return 0;
}


#include <bits/stdc++.h>
#define NOTFOUND 999999999

using namespace std;
int x, y, p, q, t;

int solv() {
	long long rng = 10000000;
	long long ans = rng;
	long long some = 1000000;
	long long mp = p;
	long long mq = q;
	long long res = NOTFOUND;
	bool b = false;
	int oldres = -1;
	int last = 0;
	long long t = 10000;
	while (t--) {
		if (mp - x <= mq - y && mp >= x && mq >= y) {
			if (last == 2)
				rng /= 2;
			oldres = res;
			res = min(res, mq - y);
			rng = min(rng, ans - 1);
			ans -= rng;
			last = 1;
		}
		else {
			if (last == 1)
				rng /= 2;
			ans += rng / 2;
			last = 2;
		}
		mp = p * ans;
		mq = q * ans;
	}
	return res;
}
int main() {
	/*freopen("C.in", "r", stdin);
	freopen("C.out", "w", stdout);*/
	cin >> t;
	while (t--) {
		cin >> x >> y >> p >> q;
		long long res = solv();
		if (res == NOTFOUND) {
			cout << -1 << endl;
		}
		else
			cout << res << endl;
	}
	return 0;
}


#include <bits/stdc++.h>

using namespace std;
int x, y, p, q, t;
unsigned long long get_min(unsigned long long xx, unsigned long long yx) {
	if (xx < yx)
		return xx;
	return yx;
}
bool ans = false;
unsigned long long  solv() {
	unsigned long long rng = (ULLONG_MAX - 100) / q;
	unsigned long long mp = p;
	unsigned long long mq = q;
	unsigned long long res = (ULLONG_MAX - 10);
	unsigned long long s = 0;
	unsigned long long e = rng;
	unsigned long long md;
	ans = false;
	unsigned long long t = 100000;
	while (s <= e) {
		md = s + (e - s) / 2;
		mp = p * md;
		mq = q * md;
		if (mp - x <= mq - y && mp >= x && mq >= y && mp <= mq) {
			res = get_min(res, mq - y);
			ans = true;
			e = md - 1;
		}
		else {
			s = md + 1;
		}
	}
	return res;
}
int main() {
	/*freopen("C.in", "r", stdin);
	freopen("C.out", "w", stdout);*/
	cin >> t;
	while (t--) {
		cin >> x >> y >> p >> q;
		long long res = solv();
		if (ans)
			cout << res;
		else
			cout << -1;
		cout << endl;
	}
	return 0;
}


#include <bits/stdc++.h>
#define NOTFOUND -1

using namespace std;
int x, y, p, q, t;

unsigned long long  solv() {
	unsigned long long rng = std::numeric_limits<unsigned long long>::max() / q;
	unsigned long long ans = 1;
	unsigned long long mp = p;
	unsigned long long mq = q;
	unsigned long long res = -1;
	unsigned long long s = 0;
	unsigned long long e = rng;
	unsigned long long md;
	int oldres = -1;
	int last = 0;
	long long t = 100000;
	while (s < e) {
		md = s + ((e - s) / 2);
		mp = p * md;
		mq = q * md;
		if (mp - x <= mq - y && mp >= x && mq >= y && mp <= mq) {
			if (res != -1)
				res = min(res, mq - y);
			else
				res = mq - y;
			e = md - 1;
		}
		else {
			s = md + 1;
		}
	}
	return res;
}
int main() {
	//freopen("C.in", "r", stdin);
	//freopen("C.out", "w", stdout);
	cin >> t;
	while (t--) {
		cin >> x >> y >> p >> q;
		long long res = solv();
		if (res == NOTFOUND) {
			cout << -1 << endl;
		}
		else
			cout << res << endl;
	}
	return 0;
}


#include <bits/stdc++.h>
#define NOTFOUND -1

using namespace std;
int x, y, p, q, t;

int solv() {
	long long rng = 10000000;
	long long ans = 1;
	long long some = 1000000;
	long long mp = p;
	long long mq = q;
	long long res = NOTFOUND;
	bool b = false;
	int oldres = -1;
	int last = 0;
	long long t = 10000;
	while (t--) {
		if (mp - x <= mq - y && mp >= x && mq >= y && mp <= mq) {
			if (last == 2)
				rng /= 2;
			oldres = res;
			if (res != -1)
				res = min(res, mq - y);
			else
				res = mq - y;
			rng = min(rng, ans - 1);
			ans -= rng;
			last = 1;
		}
		else {
			if (last == 1)
				rng /= 2;
			ans += rng / 2;
			last = 2;
		}
		mp = p * ans;
		mq = q * ans;
	}
	return res;
}
int main() {
	/*freopen("C.in", "r", stdin);
	freopen("C.out", "w", stdout);*/
	cin >> t;
	while (t--) {
		cin >> x >> y >> p >> q;
		long long res = solv();
		if (res == NOTFOUND) {
			cout << -1 << endl;
		}
		else
			cout << res << endl;
	}
	return 0;
}


#include <bits/stdc++.h>
#define NOTFOUND -1

using namespace std;
int x, y, p, q, t;

long long  solv() {
	long long rng = 99999999999999999 / q;
	long long ans = 1;
	long long mp = p;
	long long mq = q;
	long long res = -1;
	long long s = 0;
	long long e = rng;
	long long md;
	int oldres = -1;
	int last = 0;
	long long t = 100000;
	while (s <= e) {
		md = s + (abs(e - s) / 2);
		mp = p * md;
		mq = q * md;
		if (mp - x <= mq - y && mp >= x && mq >= y && mp <= mq) {
			if (res != -1)
				res = min(res, mq - y);
			else
				res = mq - y;
			e = md - 1;
		}
		else {
			s = md + 1;
		}
	}
	return res;
}
int main() {
	/*freopen("C.in", "r", stdin);
	freopen("C.out", "w", stdout);*/
	cin >> t;
	while (t--) {
		cin >> x >> y >> p >> q;
		long long res = solv();
		if (res == NOTFOUND) {
			cout << -1 << endl;
		}
		else
			cout << res << endl;
	}
	return 0;
}


#include <bits/stdc++.h>
#define NOTFOUND -1

using namespace std;
int x, y, p, q, t;

int solv() {
	unsigned long long rng = std::numeric_limits<unsigned long long>::max() / q;
	unsigned long long ans = 1;
	unsigned long long mp = p;
	unsigned long long mq = q;
	unsigned long long res = -1;
	unsigned long long s = 0;
	unsigned long long e = rng;
	unsigned long long md;
	int oldres = -1;
	int last = 0;
	long long t = 100000;
	while (t--) {
		md = s / 2 + e / 2;
		mp = p * md;
		mq = q * md;
		if (mp - x <= mq - y && mp >= x && mq >= y && mp <= mq) {
			if (res != -1)
				res = min(res, mq - y);
			else
				res = mq - y;
			e = md - 1;
		}
		else {
			s = md + 1;
		}
	}
	return res;
}
int main() {
	//freopen("C.in", "r", stdin);
	//freopen("C.out", "w", stdout);
	cin >> t;
	while (t--) {
		cin >> x >> y >> p >> q;
		long long res = solv();
		if (res == NOTFOUND) {
			cout << -1 << endl;
		}
		else
			cout << res << endl;
	}
	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 = 0 , 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) ;

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

            ll g = gcd( p , q ) ;

            p /= g ;
            q /= g ;
            ll res = bs( x , y , p , q ) ;
            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 = 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){
            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 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) ;

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

            ll g = gcd( p , q ) ;

            p /= g ;
            q /= g ;
            ll res = bs( x , y , p , q ) ;
            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 = 0 , hi = 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 ;

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

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

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

//        deb("===" , nx , 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)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 ;

        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 mid = 0 ;
//    while((nx + p*mid - x) > (ny + q*mid - y))++mid ;
//    return mid ;

    ll lo = 0LL , hi = (ll)2e9 , mid , ans = (ll)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 ;

        ll k ;

        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 = 5e25 ;

ll bs(ll nx , ll ny , ll x , ll y , ll p , ll q){
    ll lo = 0 , hi = 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 ;

        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 = 5e25 ;

ll bs(ll nx , ll ny , ll x , ll y , ll p , ll q){
    ll lo = 0 , hi = 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 ;

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

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

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

//        deb("===" , nx , 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 = 1e10 ;

ll bs(ll x , ll y , ll p , ll q){
    ll lo = 0 , 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) ;

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

            ll g = gcd( p , q ) ;

            p /= g ;
            q /= g ;
            ll res = bs( x , y , p , q ) ;
            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 = 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;
}