#include<bits/stdc++.h>
using namespace std;
const long long mod=998244353;
void solve(){
    long long x,y,a,b,tot=0;cin>>x>>y>>a>>b;
    while(x<y){
        if(1e18-b<=x){
            while(x<y){
                if(1e18/a<=x) break;
                x*=a;
                tot++;
            }
            break;
        }
        if(1e18/a<=x){
            while(x<y){
                if(1e18-b<=x) break;
                    x+=b;tot++;
            }
            break;
        }
        if(x*a>x+b) {
            tot+=(y-x)/b; 
            x+=((y-x)/b)*b;
            break;
        }
        else {
            x*=a;tot++;
        }
    }
    if(x >= y) tot--;
    cout<<tot<<endl;
}
int main()
 {
    #ifndef ONLINE_JUDGE
        freopen("input.txt", "r", stdin);
        freopen("bhar.txt", "w", stdout);
    #endif
    long long q(1);//cin>>q;
    while(q--){
        solve();
    }
    return 0;
}
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef unsigned long long ull;
#define endl '\n'
#define all(x) (x).begin(),(x).end()
#define trace(...) debug(#__VA_ARGS__, __VA_ARGS__)
template <typename Arg1>
void debug(const char* name, Arg1&& arg1){
  cerr <<fixed<<setprecision(6)<< name << ": " << arg1 << endl;
}
template <typename Arg1, typename... Args>
void debug(const char* names, Arg1&& arg1, Args&&... args){
  const char* comma = strchr(names + 1, ',');
  cerr.write(names, comma - names) << ": " << arg1 << " | ";
  debug(comma + 1, args...);
}
#define mp(x,y) make_pair(x,y)
const int INF=1000000000+5;
const int N=100+5;
const int M=3e5+5;
const ll oo=1e18+5;
const ll mod=998244353;
const long double eps = 1e-9;

ll extended_gcd(ll a,ll b,ll& x,ll& y){
	ll t;
	if(b==0){
		x=1,y=0;
		return a;
	}
	ll d=extended_gcd(b,a%b,x,y);
	t=x;
	x=y;
	y=t-a/b*y;
	return d;
}

void solve(){
	ll X,Y,P,Q;
	cin>>X>>Y>>P>>Q;
	ll ans=LLONG_MAX;
	for(int y=0;y<Y;++y){
		for(int q=0;q<Q;++q){
			//~ n*(2*X+2*Y)+X+y=m*(P+Q)+P+q
			 //~ n*u-m*v=w
			ll u=(2*X+2*Y);
			ll v=(P+Q);
			ll w=P+q-X-y;
			ll s,t;
			ll gcd=extended_gcd(u,v,s,t);
			if(w%gcd) continue;
			s*=w/gcd;
			t*=w/gcd;
			if(s>=0){
				s%=v/gcd;
			}
			else{
				s+=(-s+v/gcd-1)/(v/gcd)*(v/gcd);
			}
			ll cur=s*u+X+y;
			ans=min(ans,cur);
		}
	}
	//~ trace(ans);
	if(ans==LLONG_MAX) cout<<"infinity";
	else cout<<ans;
	cout<<endl;
}
			

int main(){
	ios::sync_with_stdio(0); cin.tie(0);
	//~ int s,t;
	//~ int d=extended_gcd(4,6,s,t);
	//~ trace(d);
	//~ trace(s,t);
	//~ trace(4*s+6*t);
	int t;
	cin>>t;
	while(t--) solve();
	return 0;	 
}


