#include<bits/stdc++.h>
using namespace std;
#define ll long long
#define pld pair<long double, int>
#define pii pair<int, int>
#define pll pair<ll, ll>
#define pb push_back
#define ff first
#define ss second
#define SZ(x) ((int)(x).size())
#define all(x) (x).begin(), (x).end()
mt19937 rng(chrono::steady_clock::now().time_since_epoch().count());

void solve(){
    ll x, y, a, b;
    cin >> x >> y >> a >> b;
    ll ans = 0;
    while(a < (x + b) / x && x * a < y){
        x = x * a;
        ans++;
    }
    ans += (y - 1 - x) / b;
    cout << ans;
}

int main(){
    ios_base::sync_with_stdio(false);
    cin.tie(nullptr);
    solve();
    return 0;
}
#include <bits/stdc++.h>
 
#define rep(i,n) for(ll i=0;i<n;++i)
#define rrep(i, n) for(ll i=n-1;i>=0;--i)
#define rep1(i, n) for(ll i=1; i<=n; ++i)
#define repitr(itr,mp) for(auto itr=mp.begin();itr!=mp.end();++itr)
#define ALL(a) (a).begin(),(a).end()

template<class T> void chmax(T &a, const T &b){if(a < b){a = b;}}
template<class T> void chmin(T &a, const T &b){if(a > b){a = b;}}

using namespace std;
using ll  = long long;
using ld = long double;
using ull = unsigned long long;
using pll = pair<ll, ll>;

const ll MOD  = 1e9 + 7;
const ll LINF = 1LL << 60;
const int INF = 1e9 + 7;
const double PI = 3.1415926535897932384626;

ll mod(ll a, ll m){return (a % m + m) % m;}

ll extgcd(ll a, ll b, ll &x, ll &y){
    if(b == 0){
        x = 1;
        y = 0;
        return a;
    }

    ll d = extgcd(b, a%b, y, x);
    y -= a/b * x;
    return d;
}

pll ChineseRem(const vector<ll> &b, const vector<ll> &m) {
    ll r = 0, M = 1;
    rep(i, (int)b.size()) {
        ll p, q;
        ll d = extgcd(M, m[i], p, q); // p is inv of M/d (mod. m[i]/d)
        if ((b[i] - r) % d != 0) return make_pair(0, -1);

        ll tmp = (b[i] - r) / d * p % (m[i]/d);
        r += M * tmp;
        M *= m[i]/d;
    }

    return make_pair(mod(r, M), M);
}

void solve(){
    ll x, y, p, q;
    cin >> x >> y >> p >> q;

    ll ans = LINF;

    for(ll t1 = x; t1 < x + y; ++t1){
        for(ll t2 = p; t2 < p + q; ++t2){
            pll res = ChineseRem({t1, t2}, {(x + y) * 2, p + q});

            if(res.second < 0)continue;
            chmin(ans, res.first);
        }
    }

    if(ans >= LINF)cout << "infinity\n";
    else cout << ans << endl;
}

int main(){
    ll t;
    cin >> t;
    rep(i, t)solve();
}