#include <bits/stdc++.h>

using namespace std;

// typedef long long int;
#define int long long
int const MAXN = 2e5 + 10, INF = 0x3f3f3f3f3f3f3f3f;
int T;
int a[5], m[5];

int qmi(int a, int b, int mod) {
    int res = 0;
    while (b > 0) {
        if (b & 1) res = (res + a) % mod;
        a = (a + a) % mod;
        b >>= 1;
    }
    return res;
}

int exgcd(int a, int b, int &x, int &y) {
    if (b == 0) {
        x = 1;
        y = 0;
        return a;
    }
    int gcd = exgcd(b, a % b, x, y);
    int tp = x;
    x = y;
    y = tp - a / b * y;
    return gcd;
}

// a, m --- 第i个方程表示x ≡ ai(mod mi)，下标从0开始
// n---- 方程个数
int excrt(int ai[], int mi[], int n) {
    int x, y, k;
    int M = mi[0], ans = ai[0];  //第一个方程的解特判
    for (int i = 1; i < n; i++) {
        int a = M, b = mi[i], c = (ai[i] - ans % b + b) % b;  // ax≡c(mod b)
        int gcd = exgcd(a, b, x, y), bg = b / gcd;
        if (c % gcd != 0) return -1;  //判断是否无解，然而这题其实不用
        x = qmi(x, c / gcd, bg);  //把x转化为最小非负整数解
        ans += x * M;             //更新前k个方程组的答案
        M *= bg;
        ans = (ans % M + M) % M;
    }
    return (ans % M + M) % M;
}

signed main() {
    cin >> T;
    while (T--) {
        int x, y, p, q;
        cin >> x >> y >> p >> q;
        int ans = INF;
        for (int i = x; i < x + y; i++) {
            for (int j = p; j < p + q; j++) {
                a[0] = i, m[0] = 2 * (x + y);
                a[1] = j, m[1] = p + q;
                // cout << CRT(a, m, n) << endl;
                // cout << "a[0] " << a[0] << "  " << "m[0] " << m[0] << endl;
                // cout << "a[1] " << a[1] << "  " << "m[1] " << m[1] << endl;
                int t = excrt(a, m, 2);
                // cout << "res: " << t << endl;
                if (t == -1) continue;
                ans = min(ans, t);
            }
        }
        if (!ans || ans == INF)
            cout << "infinity" << endl;
        else
            cout << ans << 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;
}