#include <bits/stdc++.h>

long long extGCD(long long a, long long b, long long c, long long &x, long long &y){
    long long a_gcd = a;
    long long b_gcd = b;
    std::vector< long long > A, B;
    A.push_back(a_gcd);
    B.push_back(b_gcd);
    
    while(b_gcd != 0){
        long long tmp = a_gcd % b_gcd;
        a_gcd = b_gcd;
        b_gcd = tmp;
        A.push_back(a_gcd);
        B.push_back(b_gcd);
    }
    
    if(c % a_gcd != 0){
        x = -1e18;
        y = -1e18;
        return -1;
    }
    
    std::vector< long long > X(A.size()), Y(A.size());
    X[A.size() - 1] = c / a_gcd;
    Y[A.size() - 1] = 0;
    for(int i=A.size()-2; i>=0; i--){
        X[i] = Y[i+1];
        Y[i] = X[i+1] - (A[i] / B[i]) * X[i];
    }
    
    x = X[0];
    y = Y[0];
    
    b /= a_gcd;
    
    if(x >= 0){
        return x % b;
    }else{
        long long ret;
        ret = b - std::abs(x) % b;
        if(ret == b){
            ret = 0;
        }
        return ret;
    }
    
}

int main(){
    int T;
    std::cin >> T;
    std::vector< long long > ans(T);
    
    for(int t=0; t<T; t++){
        long long X, Y, P, Q;
        std::cin >> X >> Y >> P >> Q;
        
        long long min = 9223372036854775807;
        for(int p=P; p<P+Q; p++){
            for(int x=X; x<X+Y; x++){
                long long ret1, ret2;
                long long tmp = extGCD(P+Q, 2*(X+Y), x-p, ret1, ret2);
                if(tmp != -1){
                    min = std::min(min, p + (P+Q) * tmp);
                }
            }
        }
        
        ans[t] = min;
        
    }
    
    for(int i=0; i<T; i++){
        if(ans[i] == 9223372036854775807){
            std::cout << "infinity" << std::endl;
        }else{
            std::cout << ans[i] << std::endl;
        }
    }
    
    return 0;
    
}

#include <iostream>
#include <vector>
#include <map>
#include <set>
#include <queue>
#include <algorithm>
#include <string>
#include <cmath>
#include <cstdio>
#include <iomanip>
#include <fstream>
#include <cassert>
#include <cstring>
#include <numeric>
#include <ctime>
#include <complex>
#include <bitset>
#include <random>
#include <climits>
#include <stack>

using namespace std;

typedef long long ll;
typedef long double ld;

#define int ll
#define double ld
#define loop(i, n) for(int i = 0; i < (int)n; ++i)
#define loop1(i, n) for(int i = 1; i <= (int)n; ++i)
#define F first
#define S second
#define pb push_back
#define pi pair <int, int>
#define all(x) begin(x), end(x)
#define ti tuple <int, int, int>
#define Point Vect
#define no {cout << "NO"; return;}
#define yes {cout << "Yes"; return;}
#define mkp make_pair
#define mkt make_tuple
#define cerr if(0) cerr

const int INF = 3e18;

int phi(int n) {
    int result = n;
    for (int i = 2; i * i <= n; ++i)
        if (n % i == 0) {
            while (n % i == 0)
                n /= i;
            result -= result / i;
        }
    if (n > 1)
        result -= result / n;
    return result;
}

int gcd(int a, int b) {
    while (b) {
        a %= b;
        swap(a, b);
    }
    return a;
}

int binpow(int x, int d, int mod) {
    int ans = 1;
    while (d) {
        if (d & 1)
            ans = ans * x % mod;
        x = x * x % mod;
        d >>= 1;
    }
    return ans;
}

int rev_mod(int a, int b, int m) {
    int d = gcd(a, m);
    if (b % d)
        return -1;
    a /= d, b /= d, m /= d;
    return binpow(a, phi(m) - 1, m) * b % m;
}

void solve() {
    int x, y, p, q;
    cin >> x >> y >> p >> q;
    int ans = INF, m = 2 * (x + y);
    for (int r1 = x; r1 < x + y; ++r1) {
        int cur = rev_mod(p + q, (r1 - p % m + m) % m, m);
        if (cur >= 0)
            ans = min(ans, cur * (p + q) + p);
    }
    for (int r2 = p; r2 < p + q; ++r2) {
        int cur = rev_mod(p + q, (x - r2 % m + m) % m, m);
        if (cur >= 0)
            ans = min(ans, cur * (p + q) + r2);
    }
    if (ans == INF)
        cout << "infinity\n";
    else
        cout << ans << "\n";
}

signed main() {
    ios::sync_with_stdio(false);
    cin.tie(0);
    cout.tie(0);
    int t; cin >> t; loop(i, t)
    solve();
    return 0;
}