#include <bits/stdc++.h>

using namespace std;

typedef long long ll;
typedef unsigned long long ull;
#define REP(i, n) for(int i=0; i<n; i++)
#define REPi(i, a, b) for(int i=int(a); i<int(b); i++)
#define MEMS(a,b) memset(a,b,sizeof(a))
#define mp make_pair
#define MOD(a, m) ((a % m + m) % m)
template<class T>bool chmax(T &a, const T &b) { if (a<b) { a=b; return 1; } return 0; }
template<class T>bool chmin(T &a, const T &b) { if (b<a) { a=b; return 1; } return 0; }
const ll MOD = 1e9+7;

// 負の数にも対応した 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) をリターンします
//
// ## ax + by = Aを満たす一般解(x, y)を求める
// auto d = extGcd(a, b, p, q);
// if(A % d != 0) continue;
// p *= A/d, q *= A/d;
// (x, y) = (p + k * (b/d), q + k * (a/d));
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;  
}

// 中国剰余定理
// x % m1 == b1 && x % m2 == b2であるxを計算.
// リターン値を (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(){
    ll T;
    cin >> T;
    while(T--){
        ll X, Y, P, Q;
        cin >> X >> Y >> P >> Q;

        ll T_train = 2*X + 2*Y;
        ll T_player = P + Q;

        const ll INF = LLONG_MAX;
        ll ans = INF;

        for(ll t1 = X; t1 < X+Y; t1++){
            for(ll t2 = P; t2 < P+Q; t2++){
                auto [r, m] = ChineseRem(t1, T_train, t2, T_player);
                if(m == -1) continue;
                chmin(ans, r);
            }
        }

        if(ans == INF){
            cout << "infinity" << endl;
        }
        else{
            cout << ans << endl;
        }
    }
    return 0;
}

#include <bits/stdc++.h>
#define REP(i, nn ) for(int i = 0 ; i < (int) nn; i++)
#define REPS(i, ss, nn ) for(int i = ss ; i < (int) nn; i++)
#define REV(i, ss, nn ) for(int i = ss ; i >= nn; i--)
#define deb(x) std::cout << #x << " " << x << endl;
#define debl(x) std::cout << #x << " " << x << " ";
#define all(x) x.begin(), x.end()
using namespace std;
typedef long long ll;
typedef vector<int> vi;
typedef vector<ll> vl;
namespace std{
    template<class Fun>
    class y_combinator_result{
        Fun fun_;
        public:
            template<class T>
            explicit y_combinator_result(T &&fun) : fun_(std::forward<T>(fun)){}
            template<class ...Args>
            decltype(auto) operator()(Args&&...args){
                return fun_(std::ref(*this), std::forward<Args>(args)...);
            }
    };
    template<class Fun>
    decltype(auto) y_combinator(Fun && fun){
        return y_combinator_result<std::decay_t<Fun>>(std::forward<Fun>(fun));
    }
};
template<typename T>
bool umax(T& a, T b){
    bool ret = a < b;
    if(ret) a = b;
    return ret;
}
template<typename T>
bool umin(T& a, T b){
    bool ret = a > b;
    if(ret) a = b;
    return ret;
}
struct edge{
    int to; ll cost;
    int from;
    edge(){ edge(0,0);}
    edge(int to_, ll cost_) : to(to_), cost(cost_){}
    edge(int to_, int from_, ll cost_) : to(to_), cost(cost_), from(from_){}
};

template<typename... T>
void read(T& ... a){
    ((cin >> a),...);
}

template<typename... T>
void write(T... a){
    ((cout << a),...);
}


template<typename T>
vector<T> read_array(int sz){
    vector<T> ret(sz);
    for(auto & x : ret) cin >> x;
    return ret;
}

long long gcd(long long  a, long long b){
    if( a < b) std::swap(a, b);
    if( b == 0) return a;
    return gcd(b, a%b);
}

void ext_gcd(long long a, long long b, long long& x, long long& y){
    if(b == 0){
        x = 1;
        y = 0;
        return;
    }
    if( a < b){
        ext_gcd(b, a, y, x);
    }else{
        long long tmpx, tmpy;
        ext_gcd(b, a % b, tmpx, tmpy);
        // g = b * x + y *( a - a / b * b)
        // g = y +( x - a / b * y);
        x = tmpy;
        y = tmpx - a / b * tmpy;
    }
}

//to be verified
long long mod_inverse(long long a, long long m){
    //return such that b * a = 1(mod m)
    long long x, y;
    ext_gcd(a, m, x, y);
    assert( a * x + m * y == 1);
    return ( m + x % m) % m;
}

void solve(){
    ll x, y, p, q;
    read(x,y, p, q);
    ll T1 = 2 * (x + y) ;
    ll T2 = (p + q);
    ll mxT = T1 * T2;
    ll ans = mxT;
    ll g = gcd(T1, T2);
    ll t1 = T1 / g;
    ll t2 = T2 / g;
    REP(i, y){
        //find smallest n such that T1 * n + x + i = p (mod T2)
        // T1 * n = (p - x -i) (mod T2)
        if( ( p - x -i) % g != 0){
            continue;
        }
        ll rhs = ( p - x - i) / g;
        // deb(rhs)
        rhs = (rhs % t2 + t2) % t2;
        // deb(rhs)
        ll n = mod_inverse(t1, t2);
        // deb(n)
        n = (n * rhs) % t2;
        // deb(n)
        ll res = T1 * n + x + i;
        // debl(i) deb(res)
        umin(ans, res);
    }

    REP(i, q){
        //find smallest n such that T2 * n + p + i = x (mod T1)
        // T2 * n = (x - p - i) (mod T1)
        if( (x - p - i) % g != 0){
            continue;
        }
        ll rhs = (x - p - i) / g;
        // deb(rhs)
        rhs = (rhs % t1 + t1) % t1;
        // deb(rhs)
        ll n = mod_inverse(t2, t1);
        n = (n * rhs) % t1;
        // deb(n)
        ll res = T2 * n + p + i;
        // debl(i) deb(res)
        umin(ans, res);
    }
    if(ans < mxT){
        write(ans, "\n");
    }else{
        write("infinity\n");
    }
}

int main()
{
    //making data IO Fast
    std::ios_base::sync_with_stdio(false);
    std::cin.tie(NULL);
    /****************************/
    int T = 1;
    cin >> T;
    while(T--)
        solve();
    return 0;
}

