#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <cstring>

#include <algorithm>
#include <bitset>
#include <complex>
#include <iostream>
#include <map>
#include <numeric>
#include <queue>
#include <set>
#include <stack>
#include <string>
#include <unordered_map>
#include <unordered_set>
#include <vector>

#include <cassert>
#include <functional>

using ll = long long;
using namespace std;

template<typename A, typename B>
bool chmin(A &a, const B b) {
  if (a <= b) return false;
  a = b;
  return true;
}

template<typename A, typename B>
bool chmax(A &a, const B b) {
  if (a >= b) return false;
  a = b;
  return true;
}

#ifndef LOCAL
#define debug(...) ;
#else
#define debug(...) cerr << __LINE__ << " : " << #__VA_ARGS__ << " = " << _tostr(__VA_ARGS__) << endl;

template<typename T>
ostream &operator<<(ostream &out, const vector<T> &v);

template<typename T1, typename T2>
ostream &operator<<(ostream &out, const pair<T1, T2> &p) {
  out << "{" << p.first << ", " << p.second << "}";
  return out;
}

template<typename T>
ostream &operator<<(ostream &out, const vector<T> &v) {
  out << '{';
  for (const T &item : v) out << item << ", ";
  out << "\b\b}";
  return out;
}

void _tostr_rec(ostringstream &oss) {
  oss << "\b\b \b";
}

template<typename Head, typename... Tail>
void _tostr_rec(ostringstream &oss, Head &&head, Tail &&...tail) {
  oss << head << ", ";
  _tostr_rec(oss, forward<Tail>(tail)...);
}

template<typename... T>
string _tostr(T &&...args) {
  ostringstream oss;
  int size = sizeof...(args);
  if (size > 1) oss << "{";
  _tostr_rec(oss, forward<T>(args)...);
  if (size > 1) oss << "}";
  return oss.str();
}
#endif

constexpr int mod = 1'000'000'007; //1e9+7(prime number)
constexpr int INF = 1'000'000'000; //1e9
constexpr ll LLINF = 2'000'000'000'000'000'000LL; //2e18
constexpr int SIZE = 200010;

// {a, {b, c}} : a = gcd(p, q) = bp + cq
pair<ll, pair<ll, ll>> ext_gcd(ll p, ll q) {
  if (q == 0) return {p, {1, 0}};
  auto r = ext_gcd(q, p % q);
  return {r.first, {r.second.second, r.second.first - p / q * r.second.second}};
}

/* 中国剰余定理 */

pair<ll, ll> CRT(ll a1, ll m1, ll a2, ll m2) {
  auto eg = ext_gcd(m1, m2);
  ll d = eg.first;
  if ((a2 - a1) % d) return {0, -1};
  ll m = m1 / d * m2;
  ll r = ((a1 + m1 * ((__int128_t)(a2 - a1) / d * eg.second.first % (m2 / d))) % m + m) % m;
  return {r, m};
}

void solve() {
  ll X, Y, P, Q;

  cin >> X >> Y >> P >> Q;

  ll ans = LLINF;

  for (int i = 0; i < Y; i++) {
    for (int j = 0; j < Q; j++) {
      auto r = CRT(X + i, (X + Y) * 2, P + j, P + Q);

      if (r.second != -1) {
        debug(X + i, r);
        chmin(ans, r.first);
      }
    }
  }

  if (ans == LLINF)
    puts("infinity");
  else
    printf("%lld\n", ans);
}

int main() {
  int T;

  cin >> T;

  while (T--) solve();

  return 0;
}
#include<bits/stdc++.h>
using namespace std;
const long long mod=998244353,INF=1e18;
#define ll long long
#define line cout<<"\n";
#define flush cout<<endl;
#define pll pair<ll,ll>
#define x first
#define y second
#define p_all(arr) for(auto i:arr){cout<<i<<" ";}cout<<"\n";
ll power(ll u,ll v)
{
    ll res=1;
    while(v)
    {
        if(v&1)
        {
            res=(res*u)%mod;
        }
        u=(u*u)%mod;
        v>>=1;
    }
    return res;
}
ll modInv(ll u)
{
    return power(u,mod-2);
}
int main()
{
    ios_base::sync_with_stdio(false);
    cin.tie(NULL);cout.tie(NULL);
    ll t=1;
    // cin>>t;
    for(ll test=1;test<=t;test++)
    {
        ll n,m;
        cin>>n>>m;
        if(m%2)
        {
            cout<<"0\n";
            continue;
        }
        vector<ll> fac(5005),facInv(5005);
        fac[0]=facInv[0]=1;
        for(ll i=1;i<=5000;i++)
        {
            fac[i]=(i*fac[i-1])%mod;
            facInv[i]=modInv(fac[i]);
        }
        ll dp[2][5005];
        memset(dp,0,sizeof(dp));
        dp[1][0]=1;
        auto nCr=[&](ll u,ll v)
        {
            if(u<v)
            return 0LL;
            ll res=fac[u];
            res=(res*facInv[v])%mod;
            res=(res*facInv[u-v])%mod;
            return res;
        };
        ll set=0;
        for(int i=0;(1<<i)<=m;i++)
        {
            for(int j=0;j<=n;j+=2)
            {
                ll tot=(1<<i)*j;
                for(int k=0;(k+tot)<=m;k++)
                {
                    dp[set][k+tot]=(dp[set][k+tot]+dp[1-set][k]*nCr(n,j))%mod;
                }
            }
            set^=1;
            for(int j=0;j<=n;j+=2)
            {
                for(int k=0;k<=m;k++)
                dp[set][k]=0;
            }
        }
        cout<<dp[1-set][m]<<"\n";
    }
    return 0;
}
