#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+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 <bits/stdc++.h>
#define rei register int
#define int signed long long
#pragma GCC optimize(2)
using namespace std;
int T,X,Y,P,Q;
int ans,cnt;
void exgcd(int &x,int &y,int a,int b)
{
    if(!b)
    {
        x=1;
        y=0;
        return;
    }
    exgcd(x,y,b,a%b);
    int t=x;
    x=y;
    y=t-a/b*y;
} int gcd(int x,int y){
	return !y ? x : gcd(y,x % y);
} int mul(int x,int y,int mod){
	int res = 0;
	while(y){
		if(y & 1) res = (res + x) % mod;
		x = 2 * x % mod; y >>= 1;
	} return res;
}
void solve(int d1,int mod1,int d2,int mod2){
	int rec = gcd(mod1,mod2);
	if((d2 - d1) % rec != 0) return;
	int LCM = mod1 * mod2 / gcd(mod1,mod2);
	int rate = (d2 - d1) / gcd(mod1,mod2);
	int sol1,sol2; exgcd(sol1,sol2,mod1,mod2);
	sol1 = mul(sol1,(rate % LCM + LCM) % LCM,LCM);
	int res = ((mul(sol1,mod1,LCM) + d1) % LCM + LCM) % LCM;
	ans = min(ans,res); 
}
signed main(){
	cin >> T; while(T--){
		cin >> X >> Y >> P >> Q;
		ans = (long long)9e18;
		for(rei i = X;i <= X + Y - 1;++i) 
		 for(rei j = P;j <= P + Q - 1;++j) 
			 solve(i,2 * X + 2 * Y,j,P + Q);
		if(ans == (long long)9e18) cout << "infinity" << endl; else cout << ans << endl;
	}  return 0;
}