#include<bits/stdc++.h>
using namespace std;
# define ll long long
# define read read1<ll>()
# define Type template<typename T>
Type T read1(){
	T t=0;
	char k;
	bool vis=0;
	do (k=getchar())=='-'&&(vis=1);while('0'>k||k>'9');
	while('0'<=k&&k<='9')t=(t<<3)+(t<<1)+(k^'0'),k=getchar();
	return vis?-t:t;
}
# define fre(k) freopen(k".in","r",stdin);freopen(k".out","w",stdout)
void exgcd(ll u,ll v,ll &x,ll &y){
	if(!v){x=1,y=0;return;}
	if(u<v){
		exgcd(v,u,y,x);x=-x;y=-y;
		return;
	}ll i=u/v;
	exgcd(u%v,v,x,y);
	y+=x*i;
}
int main(){
	for(int T=read;T--;){
		int x=read,y=read,p=read,q=read;
		ll a=x+y<<1,b=p+q,d=__gcd(a,b);ll ans=-1;
		for(int i=x;i<x+y;++i)
			for(int j=p;j<p+q;++j){
				ll c=j-i;//ax-by=c
				if(c/d*d!=c)continue;
				ll l=a/d,r=b/d,u=0,v=0;c/=d;//lu-rv=c
				exgcd(l,r,u,v);//lu-rv=1
				u*=c;v*=c;//lu-rv=c
				ll m=max(max((r-u-1)/r,(l-v-1)/l),0ll);
				u+=r*m,v+=l*m;
				m=max(min(u/r,v/l),0ll);
				u-=r*m;v-=l*m;
				//cout<<u*a+i<<' '<<v*b+j<<endl;
				if(!~ans||ans>v*b+j)ans=v*b+j;
			}if(~ans)cout<<ans<<endl;
			else puts("infinity");
	}
	return 0;
}

//abc193_e.cpp
//Mon Mar 15 23:09:25 2021

#include <bits/stdc++.h>
#define INTINF 2147483647
#define LLINF 9223372036854775807
#define MOD 1000000007
#define rep(i,n) for (int i=0;i<(n);++i)

using namespace std;
using ll=long long;
typedef pair<int,int> P;

inline ll mod(ll a, ll m){
	return (a%m+m)%m;
}

ll extGCD(ll a, ll b, ll &p, ll &q){
	if (b == 0){
		p = 1;
		q = 0;
		return a;
	}
	ll d = extGCD(b, a%b, q, p);
	q -= a/b*p;
	return d;
}

//中国剰余定理。
//リターンは(r,m)。x ≡ r (mod m)
//解なしの時は(0,-1)が帰ってくる。
pair<ll,ll> ChineseRem(ll b1, ll m1, ll b2, ll m2){
	ll p,q;
	ll d = extGCD(m1,m2,p,q);
	if ((b2-b1)%d!=0) return make_pair(0,-1);
	ll m = m1*(m2/d); 
	ll tmp = (b2-b1)/d*p%(m2/d);
	ll r = mod(b1+m1*tmp,m);
	return make_pair(r,m);
}

int main(){
	int t;
	cin >> t;
	rep(i,t){
		ll x,y,p,q;
		cin >> x >> y >> p >> q;
		ll ans = LLINF;
		for(ll j=x;j<x+y;j++){
			for(ll k=p;k<p+q;k++){
				pair<ll,ll> tmp = ChineseRem(j,2*x+2*y,k,p+q);
				if (tmp.second!=-1){
					ans = min(ans,tmp.first);
				}
			}
		}
		if (ans == LLINF)cout << "infinity" << endl;
		else cout << ans << endl;
	}
}