#include <bits/stdc++.h>

using namespace std;

typedef long long ll;
typedef pair<int, int> pi;

pi operator - (pi a, pi b) { return { a.first - b.first, a.second - b.second }; }
pi operator + (pi a, pi b) { return { a.first + b.first, a.second + b.second }; }
ll operator * (pi a, pi b) { return a.first * 1LL * b.first + a.second * 1LL * b.second; }

const int dy[4] = { -1, 0, 1, 0 }, dx[4] = { 0, 1, 0, -1 };

// 최대공약수, x, y
vector<ll> ee(ll a, ll b) {
	if (b == 0)
		return { a, 1, 0 };
	ll g, x, y;
	vector<ll> t = ee(b, a % b);
	g = t[0];
	x = t[1];
	y = t[2];
	return { g, y, x - (a / b) * y };
}

int main() {
	ios_base::sync_with_stdio(false);
	cin.tie(nullptr);

	int T;
	cin >> T;
	while (T--) {
		ll x, y, p, q;
		cin >> x >> y >> p >> q;

		ll mn = 9e18;
		for (int t1 = 0; t1 < y; ++t1) 
			for (int t2 = 0; t2 < q; ++t2) {
				ll a = (2 * x + 2 * y), b = -(p + q);
				ll c = p - x + t2 - t1;
				vector<ll> res = ee(a, b);
				if (c % res[0] != 0)
					continue;

				ll u = res[1] * c / res[0];

				ll k = u - u * res[0] / b * b / res[0];

				while (u != 0 && k - abs(u * b / res[0]) >= 0)
					k -= abs(b / res[0]);
				while (u != 0 && k < 0)
					k += abs(b / res[0]);

				mn = min(mn, a * k + x + t1);
			}

		if (mn == 9e18)
			cout << "infinity\n";
		else
			cout << mn << "\n";
	}

	return 0;
}
#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();
}
