#include <bits/stdc++.h>
#include <variant>

#define rep2(i,k,n) for(i64 i=(i64)(k);i<(i64)(n);i++)
#define rep(i,n) rep2(i,0,n)
#define all(x) begin(x),end(x)
#ifdef ENV_LOCAL
#define dump if (1) cerr
#else
#define dump if (0) cerr
#endif

using namespace std;
using namespace std::string_literals;
//using namespace harudake;

using i32 = int32_t;
using i64 = int64_t;
using f64 = double;
using f80 = long double;
using vi32 = vector<i32>;
using vi64 = vector<i64>;

// ax+by=gcd(a,b)
// |x|+|y| -> min
// x <= y
i64 extgcd(i64 a, i64 b, i64& x, i64& y) {
  i64 g = a; x = 1; y = 0;
  if (b) g = extgcd(b, a%b, y, x), y -= (a / b) * x;
  return g;
}

int main() {
  ios::sync_with_stdio(false);
  cin.tie(nullptr);
  i64 t;
  cin>>t;
  while(t--) {
    i64 x,y,p,q;
    cin>>x>>y>>p>>q;
    x += y;
    p += q;
    i64 x2 = x*2;
    i64 a,b;
    i64 g = extgcd(x2, p, a, b);
    b *= -1;
    dump<<g<<" "<<a<<" "<<b<<endl;
    i64 INF = 8e18;
    i64 ans = INF;
    rep2(y1,1,y+1) {
      rep2(q1,1,q+1) {
        i64 y2 = y1 + x;
        i64 yq = y2 - q1;
        if (yq % g != 0) continue;
        i64 scale = yq / g;
        i64 lcm = x2 * p / g;
        i64 pg = p / g;
        i64 a1 = a % pg * scale % pg;
        i64 t = (a1 * x2 + lcm - y2) % lcm;
        dump<<y2<<" "<<q1<<" "<<t<<endl;
        if (t < 0) {
          t = (lcm + t % lcm) % lcm;
        } else {
          t %= lcm;
        }
        ans = min(ans, t);
      }
    }
    if (ans < INF) {
      cout<<ans<<"\n";
    } else {
      cout<<"infinity"<<endl;
    }
  }
  return 0;
}

#include <bits/stdc++.h>
#define DEBUG if(0)
#define lli __int128
#define ldouble long double
using namespace std;

/*
(2x + 2y)n + x <= t < (2x + 2y)n + x + y
  range = y (max 500)
(p + q)n + p <= t < (p + q)(n + 1)
  range = q (max 500)

try all combinations of mod (500^2)

t - (2x + 2y)n - x == remY
t - (2x + 2y)n = remY + x
t = remY + x + (2x + 2y)n1

t - (p + q)n - p == remQ
t - (p + q)n = remQ + p
t = remQ + p + (p + q)n2

remQ + p + (p + q)n2 = remY + x + (2x + 2y)n1
(p + q)n2 - (2x + 2y)n1 = remY + x - remQ - p

*/

lli read() {
    lli x = 0, f = 1;
    char ch = getchar();
    while (ch < '0' || ch > '9') {
        if (ch == '-') f = -1;
        ch = getchar();
    }
    while (ch >= '0' && ch <= '9') {
        x = x * 10 + ch - '0';
        ch = getchar();
    }
    return x * f;
}
void print(lli x) {
    if (x < 0) {
        putchar('-');
        x = -x;
    }
    if (x > 9) print(x / 10);
    putchar(x % 10 + '0');
}

lli abslol(lli num)
{
  return num < 0 ? -num : num;
}

lli gcd(lli a, lli b, lli& x, lli& y)
{
  if (b == 0) {
    x = 1;
    y = 0;
    return a;
  }
  lli x1, y1;
  lli d = gcd(b, a % b, x1, y1);
  x = y1;
  y = x1 - y1 * (a / b);
  return d;
}

bool find_any_solution(lli a, lli b, lli c, lli &x0, lli &y0, lli &g)
{
  g = gcd(abslol(a), abslol(b), x0, y0);
  if (c % g) {
    return false;
  }

  x0 *= c / g;
  y0 *= c / g;
  if (a < 0) x0 = -x0;
  if (b < 0) y0 = -y0;
  return true;
}

const lli inf = LONG_LONG_MAX;
lli x, y, p, q;


lli findMinSolution(lli remY, lli remQ)
{
  // t - (2x + 2y)n - x == remY
  // t - (2x + 2y)n = remY + x
  // t = remY + x + (2x + 2y)x0

  // t - (p + q)n - p == remQ
  // t - (p + q)n = remQ + p
  // t = remQ + p + (p + q)y0

  // remQ + p + (p + q)y0 = remY + x + (2x + 2y)x0
  // -(2x + 2y)x0 + (p + q)y0 = remY + x - remQ - p
  lli x0, y0, g;
  lli a = -(2*x + 2*y), b = p + q, c = remY + x - remQ - p;
  if (find_any_solution(a, b, c, x0, y0, g))
  {
    lli t1 = remY + x + (2*x + 2*y)*x0, t2 = remQ + p + (p + q)*y0;
    bool isNegative = a / g < 0;

    // lli lo = 0, hi = 1e18;
    // while (lo < hi)
    // {
    //   lli mid = (lo + hi + 1) >> 1;
    //   if ((y0 + mid*(a / g)) != 0 && LONG_LONG_MAX / abslol(y0 + mid*(a / g)) < (p + q))
    //     hi = mid - 1;
    //   else
    //     lo = mid;
    // }
    // lli maxHi = lo;

    // lo = -1e18, hi = 0;
    // while (lo < hi)
    // {
    //   lli mid = (lo + hi) >> 1;
    //   if ((y0 + mid*(a / g)) != 0 && LONG_LONG_MAX / abslol(y0 + mid*(a / g)) < (p + q))
    //     lo = mid + 1;
    //   else
    //     hi = mid;
    // }
    // lli minLo = lo;

    lli lo = -1e18, hi = 1e18;
    while (lo < hi)
    {
      lli mid = (lo + hi + isNegative) >> 1;
      lli e = remQ + p + (p + q)*(y0 + mid*(a / g));

      // DEBUG printf("\t\t%lld %lld\n", mid, e);
      if (isNegative)
      {
        if (e >= 0) lo = mid;
        else hi = mid - 1;
      }
      else
      {
        if (e >= 0) hi = mid;
        else lo = mid + 1;
      }
    }

    x0 -= lo * b / g;
    y0 += lo * a / g;

    // DEBUG printf("\t%lld %lld - %lld %lld | %lld %lld\n", remY, remQ, t1, t2, 0LL, b / g);
    return remQ + p + (p + q)*y0;
  }
  else
    return inf;
}

int main()
{
  int t; scanf("%d", &t);
  for (int tt = 1; tt <= t; tt++)
  {
    // scanf("%lld %lld %lld %lld", &x, &y, &p, &q);
    x = read();
    y = read();
    p = read();
    q = read();
    lli ans = inf;
    for (int i = 0; i < y; i++)
      for (int j = 0; j < q; j++)
        ans = min(ans, findMinSolution(i, j));

    if (ans == inf) printf("infinity\n");
    else
    {
      print(ans);
      printf("\n");
    }
  }

  return 0;
}