#include <iostream>
#include <algorithm>
#include <vector>
#include <queue>
#include <string>

#define amax(a, b) a = std::max(a, b)
#define amin(a, b) a = std::min(a, b)

using ll = long long;

// 返り値: a と b の最大公約数
// ax + by = gcd(a, b) を満たす (x, y) が格納される
long long extGCD(long long a, long long b, long long &x, long long &y) {
    if (b == 0) {
        x = 1;
        y = 0;
        return a;
    }
    long long d = extGCD(b, a%b, y, x);
    y -= a/b * x;
    return d;
}

int main() {
    int T;
    std::cin >> T;
    while (T--) {
        ll X, Y, P, Q;
        std::cin >> X >> Y >> P >> Q;
        ll N = (X + Y) * 2, M = (P + Q);
        ll inf = N * M;
        ll ans = N * M;
        
        ll _a, _b;
        ll gcd = extGCD(N, M, _a, _b);
        const ll a = _a, b = _b;
        // std::cerr << N << ' ' << M << ' ' << a << ' ' << b << std::endl;
        // a > 0 && b <= 0
        for (ll y=0; y<Y; y++) {
            ll yoff = X + y;
            for (ll q=0; q<Q; q++) {
                ll qoff = P + q;
                ll a0 = a, b0 = b;

                if (std::abs(qoff - yoff) % gcd != 0) continue;
                ll r = (qoff - yoff) / gcd;
                a0 *= r;
                b0 *= r;
                if (a0 < 0) {
                    ll dif = (std::abs(a0) + (M / gcd) - 1) / (M / gcd);
                    // ll dif = std::abs(a0) / (M / gcd) + 1;
                    a0 += dif * M / gcd;
                    b0 -= dif * N / gcd;
                }
                a0 %= (M / gcd);
                // a > 0 && b < 0
                // std::cerr << y << ' ' << q << ' ' << a0 << ' ' << a0 * N + yoff << std::endl;
                amin(ans, a0 * N + yoff);
            }
        }
        std::cout << (ans < inf ? std::to_string(ans) : std::string("infinity")) << '\n';
        // std::cerr << std::endl;
    }
    return 0;
}
#include <bits/stdc++.h>
using namespace std;
#define pb push_back
#define eb emplace_back
#define x first
#define y second
#define FOR(i, m, n) for (ll i(m); i < n; i++)
#define DWN(i, m, n) for (ll i(m); i >= n; i--)
#define REP(i, n) FOR(i, 0, n)
#define DW(i, n) DWN(i, n, 0)
#define F(n) REP(i, n)
#define FF(n) REP(j, n)
#define D(n) DW(i, n)
#define DD(n) DW(j, n)
using ll = long long;
using ld = long double;
using pll = pair<ll, ll>;
using tll = tuple<ll, ll, ll>;
using vll = vector<ll>;
using vpll = vector<pll>;
using vtll = vector<tll>;
using gr = vector<vll>;
using wgr = vector<vpll>;
void add_edge(gr&g,ll x, ll y){ g[x].pb(y);g[y].pb(x); }
void add_edge(wgr&g,ll x, ll y, ll z){ g[x].eb(y,z);g[y].eb(x,z); }
template<typename T,typename U>
ostream& operator<<(ostream& os, const pair<T,U>& p) { 
    cerr << ' ' <<  p.x << ',' << p.y; return os; }
template <typename T> 
ostream& operator<<(ostream& os, const vector<T>& v) { 
    for(auto x: v) os << ' ' <<  x; return os; }
template <typename T> 
ostream& operator<<(ostream& os, const set<T>& v) { 
    for(auto x: v) os << ' ' <<  x; return os; }
template<typename T,typename U>
ostream& operator<<(ostream& os, const map<T,U>& v) { 
    for(auto x: v) os << ' ' <<  x; return os; }
struct d_ {
    template<typename T> d_& operator,(const T& x) {
        cerr << ' ' <<  x; return *this;}
} d_t;
#define dbg(args ...) { d_t,"|",__LINE__,"|",":",args,"\n"; }
#define deb(X ...) dbg(#X, "=", X);
#define EPS (1e-10)
#define INF (1LL<<61)
#define YES(x) cout << (x ? "YES" : "NO") << endl;
#define CL(A,I) (memset(A,I,sizeof(A)))
#define all(x) (x).begin(),(x).end()
#define rall(x) (x).rbegin(),(x).rend()

ll mod(ll a, ll b) {
  return ((a%b)+b)%b;
}
ll extended_euclid(ll a,ll b,ll &x,ll &y) {
    if (a == 0) { x = 0; y = 1; return b;}
    ll x1, y1; ll d = extended_euclid(b%a, a, x1, y1);
    x = y1 - (b / a) * x1;
    y = x1;
    return d;
}

ll mod_inverse(ll a, ll n) {
  ll x, y;
  ll d = extended_euclid(a, n, x, y);
  if (d > 1) return -1;
  return mod(x,n);
}


void linear_diophantine(ll a, ll b, ll c, ll &x, ll &y) {
  ll d = __gcd(a,b);
  if (c%d) {
    x = y = -INF;
  } else {
    x = c/d * mod_inverse(a/d, b/d);
    y = (c-a*x)/b;
  }
}

void solve() {
    ll x,y,p,q; cin >> x >> y >> p >> q;
    ll u = 2*(x+y); ll v = p+q;
    ll ret = INF;
    FOR(X,x,x+y) FOR(Y,p,p+q) {
        ll k,l;
        linear_diophantine(u,-v,Y-X, k,l);
        if(k==-INF && l==-INF) continue;
        ll g = __gcd(u,v);
        ll s = v/g;
        if(k<0) k += ((-k+s-1)/s)*s;
        else k-= s*(k/s);
        ret = min(ret, k*u+X);
    }
    cout << (ret==INF?"infinity":to_string(ret)) << endl;
}

int main(void) {
    ios_base::sync_with_stdio(false);
    ll _T; cin >> _T;
    REP(_,_T) solve();
    return 0;
}
