#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;
}

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

int64_t extgcd(int64_t a, int64_t b, int64_t& x, int64_t& y){
    int64_t d = a;
    if(b != 0){
        d = extgcd(b, a%b, y, x);
        y -= (a/b) * x;
    }else{
        x = 1; y = 0;
    }
    return d;
}

pair<int64_t, int64_t> crt(int64_t b1, int64_t m1, int64_t b2, int64_t m2){
    int64_t p, q;
    int64_t d = extgcd(m1, m2, p, q);
    if((b2-b1) % d != 0) return {-1, -1};
    int64_t m = m1 * (m2/d);
    int64_t tmp = (b2 - b1) / d * p % (m2/d);
    int64_t r = ((b1 + m1 * tmp) % m + m) % m;
    return make_pair(r, m);
}

void solve(){
    int64_t X, Y, P, Q;
    cin >> X >> Y >> P >> Q;
    int64_t INF = 8e18;
    int64_t ans = INF;
    for(int a=X; a<X+Y; a++) for(int b=P; b<P+Q; b++){
        auto res = crt(a, 2*X+2*Y, b, P+Q).first;
        if(res != -1) ans = min(ans, res);
    }
    if(ans == INF){
        cout << "infinity" << endl;
    }else{
        cout << ans << endl;
    }

}

int main(){
    int T;
    cin >> T;
    while(T--) solve();
    return 0;
}
