#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
const int N=2e5+5;
int n,m;
ll d;
pair<ll,ll> exgcd(ll a,ll b){
	if(!b){
		d=a;
		return {1,0};
	}
	pair<ll,ll> ret,ans=exgcd(b,a%b);
	ret.first=ans.second;
	ret.second=ans.first-(a/b)*ans.second;
	return ret;
}
int main(){
	ll i,j;
	int T;cin>>T;
	while(T--){
		ll x,y,p,q;cin>>x>>y>>p>>q;
		pair<ll,ll> ans=exgcd(x+y+x+y,p+q);
		ll answ=9e18;
		for(i=x+y+1;i<=x+y+y;i++){
			for(j=1;j<=q;j++){
				if((i-j)%d) continue;
				ll a=ans.first*((i-j)/d);
				ll mda=(p+q)/d,mdb=(x+y)*2/d;
				ll ta=(a%mda+mda)%mda;
				if(ta==0) ta+=mda;
				ll b=ans.second*(i-j)/d;
				ll tb=(b%mdb+mdb)%mdb;
				if(tb>=0) tb-=mdb;tb=-tb;
				answ=min(answ,max(ta*(x+y)*2-i,tb*(p+q)-j));
			}
		}
		if(answ==9e18) cout<<"infinity"<<endl;
		else cout<<answ<<endl;
	}
	return 0;
}
#include <bits/stdc++.h>
using namespace std;

#define fr(i,n) for(int i = 0; i<n; i++)
#define sz(v) (int)(v.size())
#define prin(a) cout << #a << " = " << a << endl
#define prinv(v) cout << #v << " = "; for(auto it : v) cout << it << ", "; cout << endl
#define all(v) (v).begin(),(v).end()

typedef long long ll;

#define rmin(a,b) a = min<ll>(a,b)
#define rmax(a,b) a = max<ll>(a,b)

#define fi first
#define se second

ll div(ll a, ll b, bool ceil){
	ll ans = abs(a/b);
	bool pos = (a<0)==(b<0);
	if(a%b and ceil==pos) ans++;
	if(!pos) ans*=-1;
	return ans;
}

ll gcd_ext(ll a, ll b, ll &xo, ll &yo){
	if(b==0){
		xo = 1, yo = 0;
		return a;
	}
	ll x1, y1;
	ll g = gcd_ext(b,a%b,x1,y1);
	xo = y1;
	yo = x1-(a/b)*y1;
	return g;
}

/*
Retorna qual o menor x positivo que satisfaz
a*x + b*y = c (obviamente o y correspondente eh negativo)
(ou -1 se nao existe)

Util em CRT para achar menor r positivo que
	r = ra (mod a)
	r = rb (mod b)
	->
	a*x-b*y = rb-ra
	r = a*x + ra
*/
ll qual_sol(ll a, ll b, ll c){
	ll xo, yo;
	ll g = gcd_ext(a,b,xo,yo);
	if(c%g!=0) return -1;
	c/=g, a/=g,b/=g;
	xo*=c,yo*=c;
	
	ll k = div(-xo,b,b>0);
	
	return xo+k*b;
}

ll mins(ll ra, ll a, ll rb, ll b){
	ll minx = qual_sol(a,-b,rb-ra);
	if(minx==-1) return minx;
	return a*minx+ra;
}

int main(){
	ios::sync_with_stdio(0); cin.tie(0);
	ll t; cin >> t;
	fr(tt,t){
		ll X, Y, P, Q; cin >> X >> Y >> P >> Q;
		
		ll t1 = P+Q, t2 = 2*X+2*Y;
		ll ans = LLONG_MAX;
		fr(i,Q) fr(j,Y){
			ll cand = mins(P+i,t1,X+j,t2);
			if(cand!=-1) rmin(ans,cand);
		}
		//prin(t1);
		//prin(t2);
		if(ans<LLONG_MAX){
			cout << ans << "\n";
		} else{
			cout << "infinity\n";
		}
	}
}