// abc193_e
#pragma GCC optimize ("O3")

#include <bits/stdc++.h>
#ifdef LOCAL
#include "../../debug_util/cxx-prettyprint/prettyprint.hpp"
#include "../../debug_util/rng.hpp"
#include "../../debug_util/timer.hpp"
#endif
using namespace std;

using ll = long long;
using ull = unsigned long long;
using P = pair<int, int>;

#define REP(i, n) for (int i = 0 ; i < (int)(n) ; ++i)
#define REPN(i, m, n) for (int i = m ; i < (int)(n) ; ++i)
#define REP_REV(i, n) for (int i = (int)(n) - 1 ; i >= 0 ; --i)
#define REPN_REV(i, m, n) for (int i = (int)(n) - 1 ; i >= m ; --i)
#define ALL(x) x.begin(), x.end()

#define INF (ll)(2e18)
#define MOD (1000000007)

#define print2D(h, w, arr) REP(i, h) { REP(j, w) cout << arr[i][j] << " "; cout << endl; }
#define print_line(vec, n) {for(int idx=0;idx<(n-1);idx++) cout << (vec)[idx] << " "; cout << (vec)[(n)-1] << endl;}
template<class T> void print(const T& x){cout << x << "\n";}
template<class T, class... A> void print(const T& first, const A&... rest) { cout << first << " "; print(rest...); }
//struct PreMain {PreMain(){cin.tie(0);ios::sync_with_stdio(false);cout<<fixed<<setprecision(20);}} premain;

// 拡張 Euclid の互除法
// ax + by = gcd(a, b) となる (x, y) を求め、d = gcd(a, b) を返す
ll extgcd(ll a, ll b, ll& x, ll& y) { ll d = a; if(b != 0){ d = extgcd(b, a%b, y, x); y -= (a/b) * x; } else{ x = 1; y = 0; } return d; }

// 負の数にも対応した mod
// 例えば -17 を 5 で割った余りは本当は 3 (-17 ≡ 3 (mod. 5))
// しかし単に -17 % 5 では -2 になってしまう
inline long long mod(long long a, long long m) {
    return (a % m + m) % m;
}

// 拡張 Euclid の互除法
// ap + bq = gcd(a, b) となる (p, q) を求め、d = gcd(a, b) をリターンします
long long extGcd(long long a, long long b, long long &p, long long &q) {
    if (b == 0) { p = 1; q = 0; return a; }
    long long d = extGcd(b, a%b, q, p);
    q -= a/b * p;
    return d;
}

// 中国剰余定理
// リターン値を (r, m) とすると解は x ≡ r (mod. m)
// 解なしの場合は (0, -1) をリターン
pair<long long, long long> ChineseRem(long long b1, long long m1, long long b2, long long m2) {
    long long p, q;
    long long d = extGcd(m1, m2, p, q); // p is inv of m1/d (mod. m2/d)
    if ((b2 - b1) % d != 0) return make_pair(0, -1);
    long long m = m1 * (m2/d); // lcm of (m1, m2)
    long long tmp = (b2 - b1) / d * p % (m2/d);
    long long r = mod(b1 + m1 * tmp, m);
    return make_pair(r, m);
}

int main() {
#ifdef LOCAL
    ifstream in("../arg.txt"); cin.rdbuf(in.rdbuf());
#endif

    int T;
    cin >> T;
    while (T--){
        ll X, Y, P, Q;
        cin >> X >> Y >> P >> Q;

        ll ans = -1;

        REP(j, Q){
            int i = 0;
            ll n1 = 2*X + 2*Y;
            ll b1 = X + i;
            ll n2 = P + Q;
            ll b2 = P + j;

            auto res = ChineseRem(b1, n1, b2, n2);
            if (res.second == -1){
                continue;
            }
            ll tmp = res.first;
            ans = ans == -1 ? tmp : min(ans, tmp);
        }
            
        REP(i, Y) {
            int j = 0;
            ll n1 = 2*X + 2*Y;
            ll b1 = X + i;
            ll n2 = P + Q;
            ll b2 = P + j;

            auto res = ChineseRem(b1, n1, b2, n2);
            if (res.second == -1){
                continue;
            }
            ll tmp = res.first;
            ans = ans == -1 ? tmp : min(ans, tmp);
        }

        if (ans == -1){
            print("infinity");
        }  else {
            print(ans);
        }
    }

    return 0;
}

#include <bits/stdc++.h>
#pragma GCC optimize(1)
#pragma GCC optimize(2)
#pragma GCC optimize(3,"Ofast","inline")
#define hh "\n"
#define fi first
#define se second
#define inf 2147483647
#define llf 9223372036854775807
#define For(i,a) for(auto &i:a)
#define mapa(a,b) make_pair(a,b)
#define tty template <typename T>
#define mm(a,b) memset(a,b, sizeof(a));
#define FOR(i,a,b) for(int i=a;i<=b;i++)
#define ROF(i,a,b) for(int i=a;i>=b;i--)
#define cf int t;cin>>t;while (t--) solve();
#define IOS ios_base::sync_with_stdio(0),cin.tie(0),cout.tie(0)
using namespace std;
typedef long long ll;
tty T gcd (T a, T b) {while (b ^= a ^= b ^= a %= b);  return a;}
inline char nc() {static char buf[1000000], *p1 = buf, *p2 = buf;return p1 == p2 && (p2 = (p1 = buf) + fread (buf, 1, 1000000, stdin), p1 == p2) ? EOF : *p1++;}
//#define nc getchar
tty inline void read(T &sum) {char ch = nc();T tf = 0;sum = 0;while((ch < '0' || ch > '9') && (ch != '-')) ch = nc();tf = ((ch == '-') && (ch = nc()));while(ch >= '0' && ch <= '9') sum = sum * 10+ (ch - 48), ch = nc();(tf) && (sum = -sum);}
tty inline void write(T x){char F[200];T tmp=x>0?x:-x ;if(x<0)putchar('-') ;T cnt=0 ;while(tmp>0){F[cnt++]=tmp%10+'0';tmp/=10;}while(cnt>0)putchar(F[--cnt]) ;}
tty inline T ksc(T x, T y, T mod){return ( x * y - (ll) ( (long double) x / mod*y )*mod + mod ) % mod;}
tty inline T exgcd(T a, T b, T &x, T &y) {if (!b) {x = 1,y=0;return a;}T d = exgcd(b, a % b, x, y);T t = x;x=y;y=t-(a/b)*y;return d;}

tty inline void rmqmin(T f[][20],T n){for (int i = 1; i <= n; i++)cin >> f[i][0];for (int j = 1; j <= 20; j++)for (int i = 1; i <= n; i++)if (i + (1 << j) - 1 <= n)f[i][j] = min(f[i][j - 1], f[i + (1 << (j-1))][j - 1]);}
tty inline T zymin(T f[][20],T z,T y){int x=int (log(y-z+1)/log(2));return min(f[z][x],f[y-(1<<x)+1][x]);}
tty inline void rmqmax(T f[][20], T n, int i1) {for (int i = 1; i <= n; i++)cin >> f[i][0];for (int j = 1; j <= 20; j++)for (int i = 1; i <= n; i++)if (i + (1 << j) - 1 <= n)f[i][j] = max(f[i][j - 1], f[i + (1 << (j - 1))][j - 1]);}
tty inline T zymax(T f[][20],T z,T y){int x=int (log(y-z+1)/log(2));return max(f[z][x],f[y-(1<<x)+1][x]);}
//-------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------

ll excrt(ll n , ll ai[] , ll bi[])
{
    ll x,y;
    ll M=bi[1] , ans=ai[1];
    for(int i=2;i<=n;i++)
    {
        ll a=M , b=bi[i] , c=(ai[i]-ans%b+b)%b;
        ll gcd=exgcd(a,b,x,y) , bg=b/gcd;
        if(c%gcd != 0) return -1;
        x = ksc(x,c/gcd,bg);
        ans += x * M;
        M *= bg;
        ans=(ans%M+M)%M;
    }
    return (ans%M+M)%M;
}
ll a[5],b[5];
void solve()
{
    ll x , y , p , q; cin >> x >> y >> p >> q;
    ll ans = llf;
    for (int i = x ; i < x + y ; i++){
        for (int j = p ; j < p + q ; j++){
            a[1] = i , b[1] = 2 * (x + y);
            a[2] = j , b[2] = p + q;
            ll res = excrt(2 , a , b);
            if (res == -1) continue;
            ans = min(ans , res);
        }
    }
    if (ans == llf) cout<<"infinity"<<endl;
    else cout<<ans<<endl;
}
int main()
{
    IOS;cf
    return 0;
}
