#include<bits/stdc++.h>
using namespace std;
template<typename A, typename B> ostream& operator<<(ostream &os, const pair<A, B> &p) { return os << '(' << p.first << ", " << p.second << ')';}
template<typename T_container, typename T = typename enable_if<!is_same<T_container, string>::value, typename T_container::value_type>::type> ostream& operator<<(ostream &os, const T_container &v) {os << '{'; string sep; for (const T &x : v) os << sep << x, sep = ", "; return os << '}'; }
void dbg_out() { cerr << endl; }
template<typename Head, typename... Tail> void dbg_out(Head H, Tail... T) { cerr << ' ' << H; dbg_out(T...); }
#define int long long
template<typename T>
void dbg_a(T *a,int l,int r){cout<<" {";for(int i = l;i<r;i++) cout<<a[i]<<", ";cout<<a[r]<<"}"<<endl;}
typedef long long ll;
template<typename T = int>
inline T read(){
    T f = 1,r = 0;
    char c = getchar();
    while(c<'0'||c>'9'){if(c == '-') f = -1; c = getchar();}
    while(c>='0'&&c<='9'){ r = (r<<1) + (r<<3) + c - '0';c = getchar();}
    return f*r;
}
typedef pair<int,int> PII;
#define fi first
#define se second
#define mst(a,b) memset(a,b,sizeof(a))
#define For(i,a,b) for(int i = a;i<=b;i++)
#define For_(i,a,b) for(int i = a;i>=b;i--)
#define _for(i,a) for(int i = 0;i<a;i++)
#define All(x) x.begin(),x.end()

//    For(i,1,n) For(j,1,m) printf("f[%lld][%lld] = %lld\n",i,j,f[i][j]);
 
const int N = 2e5 + 10,M = 2*N + 10,INF = 0x3f3f3f3f;
const int mod = 998244353;
int h[N],ne[M],e[M],idx;
void add(int a,int b){
    e[++idx] = b;ne[idx] = h[a];h[a] = idx;
}
int exgcd(int a,int b,int &x,int &y){
    int d = a;
    if(b){ d = exgcd(b, a%b, y, x);y -= (a/b)*x;}
    else{x = 1;y = 0;}
    return d;
}
int inv(int a,int mo){
    int x,y;
    int d = exgcd(a,mo,x,y);
    return d == 1 ? (x + mo)%mo : -1;//-1表示不存在，并对逆元调整
}
int gcd(int a,int b){
    return b?gcd(b,a%b):a;
}
bool merge(int a1,int m1,int a2,int m2,int &A,int &mo){
    int c = (a2 - a1),d = gcd(m1,m2);
    if(c % d) return 0;//gcd(m1,m2)|(a2 - a1)时才有解
    c = (c%m2 + m2)%m2;//将c变为[0,m2)之间的数
    c /= d;m1 /= d;m2 /= d;//两边除以d
    c = c*inv(m1,m2) % m2;//将m1/d 移到右边
    mo = m1*m2*d;// mo = m1 * m2 /d
    A = (c*m1%mo*d%mo + a1)%mo;
    return 1;
}
int EXCRT(int n,int a[],int mo[]){
    int a1 = a[1],m1 = mo[1],A,Mo;
    for(int i = 2;i<=n;i++){
        if(!merge(a1,m1,a[i],mo[i],A,Mo)) return -1;//不合法
        a1 = A;m1 = Mo;
    }
    return (a1 + m1)%m1;
}
int n;
void solve(){
    int x,y,p,q;
    cin>>x>>y>>p>>q;
    int a[3] = {},mo[3] = {};
    int ans = 3e18;
    For(i,x,x+y-1){
        For(j,p,p+q-1){
            a[1] = i,mo[1] = 2*(x+y);
            a[2] = j,mo[2] = p + q;
            int res = EXCRT(2,a,mo);
            if(res == -1) continue;
            ans = min(res,ans);
        }
    }
    if(ans == 3e18)cout<<"infinity"<<"\n";
    else cout<<ans<<"\n";
}
signed main(){
    int T = 1;
    T = read();
    while(T--) solve();
    system("pause");
    return 0;
}

#include <bits/stdc++.h>
// clang-format off
using namespace std; using ll =long long; using ull = unsigned long long; using pll = pair<ll,ll>; const ll INF=9e18;
void print0() {}
template<typename Head,typename... Tail> void print0(Head head,Tail... tail){cout<<fixed<<setprecision(15)<<head;print0(tail...);}
void print() { print0("\n"); }
template<typename Head,typename... Tail>void print(Head head,Tail... tail){print0(head);if(sizeof...(Tail)>0)print0(" ");print(tail...);}
using i128 = __int128_t;
// clang-format on

// 最大公約数
ll gcdf(ll a, ll b) {
    if (b == 0) {
        return a;
    }
    return gcdf(b, a % b);
}

// ax + by = gcd(a, b) となるような (x, y) を求める
// https://qiita.com/drken/items/0c88a37eec520f82b788
ll extgcd(ll a, ll b, ll& x, ll& y) {
    if (b == 0) {
        x = 1;
        y = 0;
        //return abs(a);
        return a;
    }
    ll d = extgcd(b, a % b, y, x);
    y -= a / b * x;
    return d;
}


ll divceil(ll x, ll y){
  return (x+y-1)/y;
}
void solve() {
    ll x, y, p, q;
    cin >> x >> y >> p >> q;
    ll a = 2 * x + 2 * y;
    ll b = p + q;
    ll g = gcdf(a, b);
    ll lcm = a * b / g;

    // lcmは2e18くらい
    ll ans = INF;
    for (ll i = 0; i < y; i++) {
        for (ll j = 0; j < q; j++) {
            // (2x+2y)k+x+i=(p+q)m+p+j
            // ak+x+i=bm+p+j
            // ak=bm+c
            // ak+bl=c (l=-m)
            // これは一次不定方程式で、c%gcd(a,b) == 0 なら拡張ユークリッドの互除法により解を求められる https://qiita.com/drken/items/b97ff231e43bce50199a

            ll c = p - x - i + j;
            if (c % g != 0) continue;

            ll k, l;
            extgcd(a, b, k, l);
            ll ratio = c / g;
            k = k * ratio;
            l = l * ratio;
            ll m = -l;

            // ある時刻から LCM(a,b) 秒後には、高橋君と電車は元の時刻と同じ状態になる.
            // 周期を左右にずらすことで「k,mが0以上で最小」になるよう調整する
            // (事実上CRTをやっていると思われる)
            {
                // 周期を右にずらす
                // k+f*z >= 0
                // z>=-k/f
                ll za = -i128(k) * a / lcm + 1;
                ll zb = -i128(m) * b / lcm + 1;
                ll z = max(za, zb);
                k += z * (lcm/a);
                m += z * (lcm/b);
            }
            {
                // 周期を左にずらしてk,mを0以上で最小にする
                // k-v*lcm/a >= 0
                // v <= k*a/lcm;
                ll za = -i128(k) * a / lcm;
                ll zb = -i128(m) * b / lcm;
                ll z = min(za, zb);
                k += z * (lcm / a);
                m += z * (lcm / b);
            }

            ll r = a * k + x + i;
            ans = min(ans, r);
        }
    }

    if (ans == INF) {
        print("infinity");
    } else {
        print(ans);
    }
}
int main() {
    ll t;
    cin >> t;
    for (ll i = 0; i < t; i++) {
        solve();
    }
}
