#include <iostream>
#include <vector>
#include <algorithm>
#include <queue>
#include <map>
#include <set>
#include <cmath>
#define rep(i,begin, end) for (ll i = begin; i < (ll)(end); i++)

using namespace std;
using ll = long long;

template<typename T>
using vec2 = vector<vector<T>>;
template<typename T>
using vec3 = vec2<vector<T>>;
template<typename T>
using vec4 = vec3<vector<T>>;
template<typename T>
using vec5 = vec4<vector<T>>;
template<typename T>
using vec6 = vec5<vector<T>>;


int gcd(int a, int b){
    if(b == 0)return a;
    return gcd(b, a%b);
}

ll extGCD(ll a, ll b, ll &x, ll &y){
    if(b == 0){
        x = 1;
        y = 0;
        return a;
    }
    ll g = extGCD(b, a%b, y, x);
    y -= a/b * x;
    return g;
}
//ax+by=cをみたす整数x,y
bool solveLinearEquation2(ll a, ll b, ll c, ll &x, ll &y){
    if(c % gcd(a,b) != 0)return false;
    /*
        (a/d)X+(b/d)Y=c/d
        (a/d)x+(b/d)y=1
        (a/d)(c/d)x+(b/d)(c/d)y=c/d
        X=(c/d)x-k(b/d)
        Y=(c/d)y+k(a/d)
        必要に応じてkを決める
    */
    ll d = extGCD(a,b,x,y);
    ll k = d<0 ? floor(min(((double)c/(double)b*(double)x), -(double)c/(double)a*(double)y)):
                ceil(max(((double)c/(double)b*(double)x), -(double)c/(double)a*(double)y));
    // cout<<a<<" "<<b<<" "<<c<<" "<<d<<endl;
    x = (c/d)*x-k*(b/d);
    y = (c/d)*y+k*(a/d);
    
    return true;
}
ll MOD = 1000000007;

vector<ll> mfact(1,1);

ll minv(ll a){
    ll x;ll y;
    extGCD(a, MOD, x, y);
    if(x<0){
        x+= ((-x)/MOD+1)*MOD;
        x %= MOD;
    }
    return x;
}

ll mpow(ll a,ll b){
    ll res = 1;
    while(b > 0){
        if(b%2==1)res=(res*a)%MOD;
        b/=2;
        a = (a*a)%MOD;
    }
    return res;
}

ll mfactorial(ll a){
    if(mfact.size()-1 >= a)return mfact[a];
    for(ll i = mfact.size();i<=a;i++){
        mfact.push_back((i * mfact[i-1])%MOD);
    }
    return mfact[a];
}

ll mcomb(ll n, ll r){
    return (((mfactorial(n) * minv(mfactorial(r)))%MOD) * minv(mfactorial(n-r)))%MOD;
}
ll ipow(ll a, ll b){
    ll res = 1;
    while(b > 0){
        if(b % 2 == 1)res*=a;
        a*=a;
        b/=2;
    }
    return res;
}

void factorize(map<ll,ll>& fac, vector<ll>& sieved, ll a){
    while(a != 1){
        if(fac.find(sieved[a]) == fac.end())fac[sieved[a]] = 0;
        fac[sieved[a]]++;
        a/=sieved[a];

    }
}
void primesieve(vector<ll>& res,ll n){
    res = vector<ll>(n+1, -1);
    for(int i = 2;i <= n;i++){
        if(res[i] != -1)continue;
        res[i] = i;
        for(int j = 2;j*i <= n;j++){
            res[j*i] = i;
        }
    }
}
vector<ll> primesuntil(ll n){
    vector<ll> res;
    vector<ll> sieved;
    primesieve(sieved, n);
    for(int i = 2;i <= n;i++){
        if(sieved[i] == i)res.push_back(i);
    }
    return res;
}



void solve(){
    // cout<<"##########"<<endl;
    ll X,Y,P,Q;cin>>X>>Y>>P>>Q;
    ll res = -1;
    rep(q,0,Q){
        rep(y,0,Y){
            ll s,t;
            if(!solveLinearEquation2(2*X+2*Y, -(P+Q), P+q-X-y, s,t))continue;
            ll a = (2*X+2*Y)*s+X+y;
            if(res == -1)res = a;
            res = min(res,a);
            // cout<<a<<endl;
            // cout<<s<<" "<<t<<endl;
            // cout<<"!!!!!!!!!!!!!!!!"<<endl;

            
        }
    }

    cout<<(res==-1 ? "infinity" : to_string(res))<<endl;
}

int main(){
    int T;cin>>T;
    rep(i,0,T)solve();
}

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