#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;
    
}

#pragma GCC optimize("Ofast")
#define _USE_MATH_DEFINES
#include "bits/stdc++.h"

using namespace std;

using ll = int64_t;
using ld = long double;
using vi = vector<int>;
using vl = vector<ll>;
using vvi = vector<vector<int>>;
using pii = pair<int, int>;
using pll = pair<ll, ll>;
using vpii = vector<pii>;
using vpll = vector<pll>;

constexpr char newl = '\n';
constexpr double eps = 1e-10;

#define FOR(i,a,b) for (int i = (a); i < (b); i++)
#define F0R(i,b) FOR(i,0,b)
#define RFO(i,a,b) for (int i = ((b)-1); i >=(a); i--)
#define RF0(i,b) RFO(i,0,b)
#define show(x) cout << #x << " = " << x << '\n';
#define rng(a) a.begin(),a.end()
#define rrng(a) a.rbegin(),a.rend()
#define sz(x) (int)(x).size()
#define YesNo {cout<<"Yes";}else{cout<<"No";}
#define YESNO {cout<<"YES";}else{cout<<"NO";}
#define v(T) vector<T>
#define vv(T) vector<vector<T>>

template<typename T1, typename T2> inline void chmin(T1& a, T2 b) { if (a > b) a = b; }
template<typename T1, typename T2> inline void chmax(T1& a, T2 b) { if (a < b) a = b; }
template<class T> bool lcmp(const pair<T, T>& l, const pair<T, T>& r) {
  return l.first < r.first;
}
template<class T> istream& operator>>(istream& i, v(T)& v) {
  F0R(j, sz(v)) i >> v[j];
  return i;
}
template<class A, class B> istream& operator>>(istream& i, pair<A, B>& p) {
  return i >> p.first >> p.second;
}
template<class A, class B, class C> istream& operator>>(istream& i, tuple<A, B, C>& t) {
  return i >> get<0>(t) >> get<1>(t) >> get<2>(t);
}
template<class T> ostream& operator<<(ostream& o, const pair<T, T>& v) {
  o << "(" << v.first << "," << v.second << ")";
  return o;
}
template<class T> ostream& operator<<(ostream& o, const vector<T>& v) {
  F0R(i, v.size()) {
    o << v[i] << ' ';
  }
  o << newl;
  return o;
}
template<class T> ostream& operator<<(ostream& o, const set<T>& v) {
  for (auto e : v) {
    o << e << ' ';
  }
  o << newl;
  return o;
}
template<class T1, class T2> ostream& operator<<(ostream& o, const map<T1, T2>& m) {
  for (auto& p : m) {
    o << p.first << ": " << p.second << newl;
  }
  o << newl;
  return o;
}

#if 1

ll gcd(ll a, ll b) { return b ? gcd(b, a % b) : a; }
ll lcm(ll a, ll b) { return a / gcd(a, b) * b; }

ll extGCD(ll a, ll b, pll& out) {
  ll l = a, r = b;
  ll la = 1, lb = 0, ra = 0, rb = 1;

  while (true) {

    // l / r = c ... d
    ll c = l / r, d = l - r * c;

    if (!d) {
      out = { ra,rb };
      return r;
    }

    la = la - ra * c;
    lb = lb - rb * c;
    swap(la, ra);
    swap(lb, rb);
    l = r; r = d;
  }
}

ll mod(ll a, ll b) {
  a %= b;
  if (a < 0) a += b;
  return a;
}

ll solve() {
  ll X, Y, P, Q;
  cin >> X >> Y >> P >> Q;

  /*
  i = [0,Y)
  j = [0,Q)
  2(X+Y)a+X + i == (P+Q)b+P + j

  (P+Q)b + P+j == X+i mod 2(X+Y)
  (P+Q)b == X+i-P-j mod 2(X+Y)

  */

  ll a = 2 * (X + Y), c = (P + Q);

  pll out;
  ll g = extGCD(a, c, out);
  //show(g);
  //show(out);
  a /= g; c /= g;
  // mod a における c の逆数
  ll e = out.second;
  if (e < 0) e = (-e * (a - 1)) % a;
  //cout << "1 / " << c << " = " << e << " mod " << a << newl;

  ll re = INT64_MAX;
  F0R(i, Y) {
    F0R(j, Q) {
      ll f = X + i - P - j, b;
      if (!f) b = 0;
      else if (f % g) continue;
      else {
        f /= g;
        if (f < 0) f += (-f+a-1)/a*a; // -f//a*a
        b = (f * e) % a;
      }
      chmin(re, (c * b)*g + P + j);
    }
  }

  return re >= INT64_MAX ? -1 : re;
}

// INSERT ABOVE HERE
signed main() {
  cin.tie(0);
  ios_base::sync_with_stdio(false);

  int T; cin >> T;
  F0R(i, T) {
    auto re = solve();
    if (re < 0) {
      cout << "infinity";
    }
    else {
      cout << re;
    }
    cout << newl;
  }
}
#endif
