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

// ap + bq = gcd(a, b) なる (p, q) を求め d = gcd(a, b) を返す
ll extgcd(const ll a, const 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;
}

// x mod s = a, x mod t = b なる x >= 0 の最小値を求める。存在しなければ -1
ll solve(const ll a, const ll b, const ll s, const ll t){
    ll p, q;
    const ll d = extgcd(s, t, p, q);
    if ((a-b) % d != 0) return -1;
    const ll ret = a + s * (((b - a) / d * p) % (t / d));
    const ll s_lcm_t = s * t / d;
    return (ret % s_lcm_t + s_lcm_t) % s_lcm_t;
}

int main(){
    int t;
    cin >> t;
    for(int ti=0; ti<t; ++ti){
        ll x, y, p, q;
        cin >> x >> y >> p >> q;
        ll ret = -1;
        for(ll yi = 0; yi < y; ++yi){
            for(ll qi = 0; qi < q; ++qi){
                const ll v = solve(x + yi, p + qi, (x + y) * 2, p + q);
                if (v >= 0){
                    if(ret == -1){
                        ret = v;
                    }else{
                        ret = min(ret, v);
                    }
                }
            }
        }
        if(ret == -1){
            cout << "infinity" << endl;
        }else{
            cout << ret << endl;
        }
    }
    return 0;
}
#include <bits/stdc++.h>
#define REP(i, n) for (int i = 0; i < (int)(n); 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;}}

int main(){
    int T;
    cin>>T;
    string ans = "";
    REP(i,T){
        ll t = LLONG_MAX;
        ll X,Y,P,Q;
        scanf("%lld %lld %lld %lld",&X,&Y,&P,&Q);
        ll a=(X+Y)<<1, b=P+Q;
        ll x, y;
        ll d = gcd(a, -b, x, y);
        ll u = b/d, v = a/d;
        ll stop0=P-X, stop1=P-X+Q-1LL;
        ll wake0=P-X-Y+1LL, wake1=P-X;
        ll from=min(stop0, wake0), to=max(stop1, wake1);
        IFOR(z, from, to){
            if(mod(z,d)!=0LL)continue;
            if(stop0<=z && z<=stop1){
                chmin(t,a*mod(z/d*x,u)+X);
            }else{
                chmin(t, b*mod(z/d*y,v)+P);
            }
        }
        ans += (t==LLONG_MAX ? "infinity" : (to_string(t)))+"\n";
    }

    cout << ans;

    return 0;
}