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

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;
}
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;
}
int gcd(int a, int b){
    if(b == 0)return a;
    return gcd(b, a%b);
}



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 d = gcd(2*X+2*Y, -(P+Q));
            if((P+q-X-y) % d != 0)continue;
            // cout<<(P+q-X-y)<<endl;
            // cout<<d<<endl;
            ll s,t;
            extGCD((2*X+2*Y)/d, -(P+Q)/d, s,t);
            // cout<<s<<" "<<t<<endl;
            s *= (P+q-X-y)/d;
            t *= (P+q-X-y)/d;
            ll k = d<0 ? floor(min(-(double)d*(double)s/(double)(P+Q), -(double)d*(double)t/(double)(2*X+2*Y)))
                        : ceil(max(-(double)d*(double)s/(double)(P+Q), -(double)d*(double)t/(double)(2*X+2*Y)));
            // cout<<k<<endl;
            s += k*(P+Q)/d;
            t += k*(2*X+2*Y)/d;
            // cout<<2*X+2*Y<<" "<< (P+Q)<<" "<<s<<" "<<t<<endl;
            
            // cout<<2*X+2*Y<<" "<< (P+Q)<<" "<<s<<" "<<t<<endl;
            ll a = (2*X+2*Y)*s+X+y;
            if(res == -1)res = a;
            res = min(res,a);
            // cout<<a<<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;

typedef long long ll;
typedef unsigned long long ull;
#define REP(i, n) for(int i=0; i<n; i++)
#define REPi(i, a, b) for(int i=int(a); i<int(b); i++)
#define MEMS(a,b) memset(a,b,sizeof(a))
#define mp make_pair
#define MOD(a, m) ((a % m + m) % m)
template<class T>bool chmax(T &a, const T &b) { if (a<b) { a=b; return 1; } return 0; }
template<class T>bool chmin(T &a, const T &b) { if (b<a) { a=b; return 1; } return 0; }
const ll MOD = 1e9+7;

// 負の数にも対応した mod
// 例えば -17 を 5 で割った余りは本当は 3 (-17 ≡ 3 (mod. 5))
// しかし単に -17 % 5 では -2 になってしまう
inline long long mod(long long a, long long m) {
    return (a % m + m) % m;
}

// 拡張 Euclid の互除法
// ap + bq = gcd(a, b) となる (p, q) を求め、d = gcd(a, b) をリターンします
//
// ## TODO : 要確認
// ## ax + by = Aを満たす一般解(x, y)を求める
// auto d = extGcd(a, b, p, q);
// if(A % d != 0) continue;
// p *= A/d, q *= A/d;
// (x, y) = (p + k * (b/d), q - k * (a/d));
long long extGcd(long long a, long long b, long long &p, long long &q) {  
    if (b == 0) { p = 1; q = 0; return a; }  
    long long d = extGcd(b, a%b, q, p);  
    q -= a/b * p;  
    return d;  
}

int main(){
    ll T;
    cin >> T;
    while(T--){
        ll X, Y, P, Q;
        cin >> X >> Y >> P >> Q;

        ll T_train = 2*X + 2*Y;
        ll T_player = P + Q;

        const ll INF = LLONG_MAX;
        ll ans = INF;
        for(ll t1 = X; t1 < X+Y; t1++){
            for(ll t2 = P; t2< P+Q; t2++){
                //// t1 - t2 = -T_train * n + T_player * m;
                ll n, m;
                auto d = extGcd(T_train, T_player, n, m);
                ll rhs = t1 - t2;
                if(rhs % d != 0) continue;
                n *= -rhs/d, m *= rhs/d;
                n = mod(n, T_player/d);
                //printf("%lld, %lld\n", t1 + T_train * n, t2 + T_player * m);
                chmin(ans, t1 + T_train * n);
            }
        }


        if(ans == INF){
            cout << "infinity" << endl;
        }
        else{
            cout << ans << endl;
        }
    }
    return 0;
}
