#include<bits/stdc++.h>
using namespace std;
#define MAXN 100005
#define lowbit(x) (x&-x)
#define reg register
#define mkpr make_pair
#define fir first
#define sec second
typedef long long LL;
typedef unsigned long long uLL;
const LL INF=0x7f7f7f7f7f7f7f7f;
const int mo=998244353;
const int zero=500;
const LL jzm=2333;
const int iv2=499122177;
const double Pi=acos(-1.0);
typedef pair<int,int> pii;
const double PI=acos(-1.0);
template<typename _T>
_T Fabs(_T x){return x<0?-x:x;}
template<typename _T>
void read(_T &x){
	_T f=1;x=0;char s=getchar();
	while(s>'9'||s<'0'){if(s=='-')f=-1;s=getchar();}
	while('0'<=s&&s<='9'){x=(x<<3)+(x<<1)+(s^48);s=getchar();}
	x*=f;
}
template<typename _T>
void print(_T x){if(x<0){x=(~x)+1;putchar('-');}if(x>9)print(x/10);putchar(x%10+'0');}
int t;LL X,Y,P,Q,x[1005],y[1005],d[1005];
LL exgcd(LL a,LL b,LL &x,LL &y){
	if(!b){x=1;y=0;return a;}
	LL d=exgcd(b,a%b,y,x);y-=x*(a/b);return d;
}
signed main(){
	read(t);
	while(t--){
		read(X);read(Y);read(P);read(Q);
		LL A=P+Q,B=2ll*X+2ll*Y,res=INF;
		for(LL i=-Q;i<=Y;i++)d[i+zero]=exgcd(A,B,x[i+zero],y[i+zero]);
		for(LL i=P;i<P+Q;i++)
			for(LL j=X;j<X+Y;j++){
				LL t=j-i,d1=d[t+zero-X+P];if(t%d1!=0)continue;
				LL ax=t/d1*x[t+zero-X+P],ay=i/d1*y[t+zero-X+P],a1=B/d1,b1=A/d1,k;
				if(ax<0)k=(a1-ax)/a1,ax+=k*a1,ay-=k*b1;
				if(ax>=0)k=(ax-1)/a1,ax-=k*a1,ay+=k*b1;
				if(ay>0)k=(ay-1)/b1,ax+=k*a1,ay-=k*b1;
				res=min(ax%a1*A+i,res);
			}
		for(LL i=-Q;i<=Y;i++)d[i+zero]=x[i+zero]=y[i+zero]=0;
		if(res>INF-1)puts("infinity");else printf("%lld\n",res);
	}
	return 0;
}
/*
[(2X+2Y)n+X,(2X+2Y)n+X+Y)
[(P+Q)m+P,(P+Q)(m+1))
(X-P)<=(P+Q)m%(2X+2Y)<(X+Y-P)
(P+Q)x+(2X+2Y)y=+j-i
*/
#include <bits/stdc++.h>
using namespace std;

typedef long long ll;

typedef pair<int, int> pii;
typedef pair<ll, int> pli;
typedef pair<ll, ll> pll;

#define mp make_pair
#define fr first
#define sc second

int lg(ll n) {
    assert(n > 0);
    int ans = -1;
    while (n) {
        ans++;
        n >>= 1;
    }
    return ans;
}

tuple<ll, ll, ll> extgcd(ll a, ll b) {
    // ax + by = gcd(a, b)
    ll x = 1, y = 0;
    ll x1 = 0, y1 = 1, a1 = a, b1 = b;
    while (b1) {
        ll q = a1 / b1;
        tie(x, x1) = make_tuple(x1, x - q * x1);
        tie(y, y1) = make_tuple(y1, y - q * y1);
        tie(a1, b1) = make_tuple(b1, a1 - q * b1);
    }
    // return a1;
    return make_tuple(a1, x, y);
}

ll renorm(ll num, ll mod) {
    num %= mod;
    return num < 0 ? num + mod : num;
}
ll crt_two(ll r1, ll m1, ll r2, ll m2) {
    r1 = renorm(r1, m1);
    r2 = renorm(r2, m2);
    if (m2 == m1) {
        if (r1 == r2) {
            return r1;
        } else {
            return 9e18;
        }
    }
    // x1 = r1 + m1 * k1, x2 = r2 + m2 * k2
    // m1 * k1 - m2 * k2 = r2 - r1
    auto [g, k1, k2] = extgcd(m1, m2);
    if ((r2 - r1) % g) {
        return 9e18;
    }
    // return renorm(r1 + (r2 - r1) / g * m1 * k1, m1 / g * m2);
    return renorm(renorm((r2 - r1) / g * k1, m2 / g) * m1 + r1, m1 / g * m2);
}

void solve() {
    ll X, Y, P, Q;
    cin >> X >> Y >> P >> Q;
    ll ans = 9e18;
    for (ll t1 = X; t1 < X + Y; t1++) {
        for (ll t2 = P; t2 < P + Q; t2++) {
            // ans % 2(X+Y) = t1
            // ans % (P+Q) = t2
            ans = min(ans, crt_two(t1, 2 * (X + Y), t2, P + Q));
        }
    }
    if (ans == 9e18)
        cout << "infinity" << '\n';
    else
        cout << ans << '\n';
}

int main() {
    ios_base::sync_with_stdio(0);
    cin.tie(0);
    cout.tie(0);
#ifndef ONLINE_JUDGE
    freopen("input.txt", "r", stdin);
    freopen("output.txt", "w", stdout);
#endif

    int t = 1;
    cin >> t;
    while (t--) solve();

    return 0;
}
