#include <bits/stdc++.h>
using namespace std;
#define pb push_back
#define eb emplace_back
#define x first
#define y second
#define FOR(i, m, n) for (ll i(m); i < n; i++)
#define DWN(i, m, n) for (ll i(m); i >= n; i--)
#define REP(i, n) FOR(i, 0, n)
#define DW(i, n) DWN(i, n, 0)
#define F(n) REP(i, n)
#define FF(n) REP(j, n)
#define D(n) DW(i, n)
#define DD(n) DW(j, n)
using ll = long long;
using ld = long double;
using pll = pair<ll, ll>;
using tll = tuple<ll, ll, ll>;
using vll = vector<ll>;
using vpll = vector<pll>;
using vtll = vector<tll>;
using gr = vector<vll>;
using wgr = vector<vpll>;
void add_edge(gr&g,ll x, ll y){ g[x].pb(y);g[y].pb(x); }
void add_edge(wgr&g,ll x, ll y, ll z){ g[x].eb(y,z);g[y].eb(x,z); }
template<typename T,typename U>
ostream& operator<<(ostream& os, const pair<T,U>& p) { 
    cerr << ' ' <<  p.x << ',' << p.y; return os; }
template <typename T> 
ostream& operator<<(ostream& os, const vector<T>& v) { 
    for(auto x: v) os << ' ' <<  x; return os; }
template <typename T> 
ostream& operator<<(ostream& os, const set<T>& v) { 
    for(auto x: v) os << ' ' <<  x; return os; }
template<typename T,typename U>
ostream& operator<<(ostream& os, const map<T,U>& v) { 
    for(auto x: v) os << ' ' <<  x; return os; }
struct d_ {
    template<typename T> d_& operator,(const T& x) {
        cerr << ' ' <<  x; return *this;}
} d_t;
#define dbg(args ...) { d_t,"|",__LINE__,"|",":",args,"\n"; }
#define deb(X ...) dbg(#X, "=", X);
#define EPS (1e-10)
#define INF (1LL<<61)
#define YES(x) cout << (x ? "YES" : "NO") << endl;
#define CL(A,I) (memset(A,I,sizeof(A)))
#define all(x) (x).begin(),(x).end()
#define rall(x) (x).rbegin(),(x).rend()

ll mod(ll a, ll b) {
  return ((a%b)+b)%b;
}
ll extended_euclid(ll a,ll b,ll &x,ll &y) {
    if (a == 0) { x = 0; y = 1; return b;}
    ll x1, y1; ll d = extended_euclid(b%a, a, x1, y1);
    x = y1 - (b / a) * x1;
    y = x1;
    return d;
}

ll mod_inverse(ll a, ll n) {
  ll x, y;
  ll d = extended_euclid(a, n, x, y);
  if (d > 1) return -1;
  return mod(x,n);
}


void linear_diophantine(ll a, ll b, ll c, ll &x, ll &y) {
  ll d = __gcd(a,b);
  if (c%d) {
    x = y = -INF;
  } else {
    x = c/d * mod_inverse(a/d, b/d);
    y = (c-a*x)/b;
  }
}

void solve() {
    ll x,y,p,q; cin >> x >> y >> p >> q;
    ll u = 2*(x+y); ll v = p+q;
    ll ret = INF;
    FOR(X,x,x+y) FOR(Y,p,p+q) {
        ll k,l;
        linear_diophantine(u,-v,Y-X, k,l);
        if(k==-INF && l==-INF) continue;
        ll g = __gcd(u,v);
        ll s = v/g;
        if(k<0) k += ((-k+s-1)/s)*s;
        else k-= s*(k/s);
        ret = min(ret, k*u+X);
    }
    cout << (ret==INF?"infinity":to_string(ret)) << endl;
}

int main(void) {
    ios_base::sync_with_stdio(false);
    ll _T; cin >> _T;
    REP(_,_T) solve();
    return 0;
}

#include<bits/stdc++.h>
using namespace std;

tuple<long long, long long, long long> ext_gcd(long long a, long long b) {
  if (b == 0) return {a, 1, 0};
  auto [gcd, y, x] = ext_gcd(b, a%b);
  y -= a/b * x;
  return {gcd, x, y}; // a*x + b*y = gcd
}

pair<long long,long long> crt(const vector<long long> &r, const vector<long long> &m) {
  /* return x that satisfies x = a mod b, for a in r, for b in m */
  /* O(n log lcm(m[i]))*/
  assert(r.size() == m.size());
  auto mod=[&](long long a,long long k){return (a%k+k)%k;}; // a mod k for negative a
  long long y = 0, lcm = 1; int n = r.size();
  for(int i=0; i<n; ++i) {
    assert(m[i] > 0);
    auto [g, p, q] = ext_gcd(lcm, m[i]);
    long long ry = mod(r[i],m[i]) - y;
    if(ry % g) return make_pair(0,0); // no solution exists
    long long s = ry / g * p % (m[i]/g);
    y = mod(y+lcm*s, lcm*(m[i]/g));
    lcm *= m[i] / g;
  }
  return make_pair(y, lcm); // x = y mod lcm
}

int main() {
  int T; cin >> T;
  while(T--) {
    int x,y,p,q; cin >> x >> y >> p >> q;
    const long long INF = 2e18;
    long long ans = INF;
    for(int a=x; a<x+y; ++a) for(int b=p; b<p+q; ++b) {
      // t = a mod (2x+2y)
      // t = b mod (p+q)
      vector<long long> r(2),m(2);
      r[0] = a, r[1] = b, m[0] = 2*(x+y), m[1] = p+q;
      auto [t,lcm] = crt(r,m);
      if(lcm) ans = min(ans,t);
    }
    if (ans == INF) cout << "infinity" << endl;
    else cout << ans << endl;
  }
}
