#include <bits/stdc++.h>
#define REP(i, n) for (int i = 0; i < (int)(n); i++)
#define FOR(i, a, b) for (long long i = (a); i < (long long)(b); i++)
#define IFOR(i, a, b) for (long long i = (a); i <= (long long)(b); i++)
using namespace std;
using VL = vector<long long>;
using ll = int64_t;
ll mod(ll x,ll m){ll y=x%m;return (y>=0LL)?y:y+m;}
template<class T>bool chmin(T& a, T b){if(a>b){a=b;return true;}else return false;}
ll gcd(ll a,ll b,ll& x,ll& y)
{if(a){ll d=gcd(b%a,a,x,y);y-=(b/a)*x;swap(x,y);if(d<0LL){d=-d;x=-x;y=-y;}return d;}else{x=0;y=1;return b;}}

ll B(ll X, ll Y, ll n){
    return (2LL*X+2LL*Y)*n+X;
}

ll W(ll P, ll Q, ll n){
    return (P+Q)*n+P;
}

int main(){
    int T;
    cin>>T;
    string ans = "";
    REP(i,T){
        ll t = LLONG_MAX;
        ll X,Y,P,Q;
        cin>>X>>Y>>P>>Q;
        ll x, y;
        ll d = gcd(2LL*(X+Y), -(P+Q), x, y);
        ll u = (P+Q)/d, v = 2LL*(X+Y)/d;
        IFOR(z, P-X, P-X+Q-1LL){
            if(mod(z,d)!=0LL)continue;
            ll n = mod(z/d*x,u);
            chmin(t,B(X,Y,n));
        }
        IFOR(z, P-X-Y+1LL, P-X){
            if(mod(z,d)!=0LL)continue;
            ll m = mod(z/d*y,v);
            chmin(t, W(P,Q,m));
        }
        if(t==LLONG_MAX){
            ans += "infinity\n";
        }else{
            ans += to_string(t) + "\n";
        }
    }

    cout << ans;

    return 0;
}
#include <cstdio>
#include <algorithm>
using namespace std;
int cm;
int g;
int jl;
int m1;
long long zm;
long long get(int x,int y){
	if((y-x)%g!=0)
		return 4e18;
	int tmp=(y-x)/g;
	long long tmpans=(1ll*jl*tmp%cm+cm)%cm;
	return (1ll*tmpans*m1+x)%zm;
}
void exgcd(int a,int b,int &g,int &x,int &y){
	if(!b){
		x=1;
		y=0;
		g=a;
		return;
	}
	exgcd(b,a%b,g,y,x);
	y-=a/b*x;
}
int main(){
	int T;
	scanf("%d",&T);
	for(int jsjs=1;jsjs<=T;jsjs++){
		int x,y,p,q;
		scanf("%d%d%d%d",&x,&y,&p,&q);
		int tmp;
		exgcd(2*x+2*y,p+q,g,jl,tmp);
		m1=2*x+2*y;
		cm=(p+q)/g;
		zm=1ll*(2*x+2*y)/g*(p+q);
		long long ans=4e18+10;
		for(int now1=x;now1<x+y;now1++)
			for(int now2=p;now2<p+q;now2++)
				ans=min(ans,get(now1,now2));
		if(ans>3e18)
			printf("infinity\n");
		else
			printf("%lld\n",ans);
	}
	return 0;
}