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

typedef long long ll;
typedef long double ld;
typedef unsigned long long ul;
typedef unsigned int ui;
typedef pair<int,int> pii;
typedef pair<ll,ll> pll;
typedef tuple<int,int,int> tiii;

//#define x first
//#define y second
#define fastio ios_base::sync_with_stdio(false);cin.tie(nullptr)
#define fopen freopen("input.txt", "r", stdin)
#define eb emplace_back
#define em emplace
#define prec(a) cout << fixed;cout.precision(a);
#define all(a) (a).begin(), (a).end()


const ll INF = 2e18;
const int inf = 2e9;
template<class T>
void pr(T t) {cerr << t << " ";}
template<class T, class ...Args>
void pr(T a, Args ...args) {cerr << a << " ";pr(args...);}
template<class ...Args>
void prl(Args ...args) {pr(args...);cerr << endl;}

const ll mxN  = 1e6 + 10;
const ll mod = 998244353;
const double eps = 1e-14;

struct Point {
    double x, y;
    Point(double a = 0, double b = 0){
        x = a, y = b;
    }
    Point operator - (const Point &a) const {
        return Point(x - a.x , y - a.y);
    }
    Point operator + (const Point &a) const {
        return Point(x + a.x, y + a.y);
    }
    Point operator / (const double &a) const{
        return Point(x / a, y / a);
    }
    Point operator * (const double &a) const{
        return Point(x * a, y * a);
    }
    double operator * (const Point &a) const{
        return x * a.y - y * a.x;
    }
    double operator ^ (const Point &a) const{
        return x * a.x + y * a.y;
    }
};

typedef Point Vector;

struct Line {
    Point x, y;
    Line () {}
    Line(Point a, Point b){
        x = a, y = b;
    }
};


int main()
{
   fastio;
   ll n;
   cin >> n;
   ll ans = 1e18;
   for(int i=0;i<n;i++)
   {
       ll a, p, x;
       cin >> a >> p >> x;
       if(a < x)
       {
           ans = min(ans, p);
       }
   }
   cout << (ans == (ll) 1e18 ? -1 : ans) << endl;

}



#include<bits/stdc++.h>
using namespace std;
# define ll long long
# define read read1<ll>()
# define Type template<typename T>
Type T read1(){
	T t=0;
	char k;
	bool vis=0;
	do (k=getchar())=='-'&&(vis=1);while('0'>k||k>'9');
	while('0'<=k&&k<='9')t=(t<<3)+(t<<1)+(k^'0'),k=getchar();
	return vis?-t:t;
}
# define fre(k) freopen(k".in","r",stdin);freopen(k".out","w",stdout)
void exgcd(ll u,ll v,ll &x,ll &y){
	if(!v){x=1,y=0;return;}
	if(u<v){
		exgcd(v,u,y,x);x=-x;y=-y;
		return;
	}ll i=u/v;
	exgcd(u%v,v,x,y);
	y+=x*i;
}
int main(){
	for(int T=read;T--;){
		int x=read,y=read,p=read,q=read;
		ll a=x+y<<1,b=p+q,d=__gcd(a,b);ll ans=-1;
		for(int i=x;i<x+y;++i)
			for(int j=p;j<p+q;++j){
				ll c=j-i;//ax-by=c
				if(c/d*d!=c)continue;
				ll l=a/d,r=b/d,u=0,v=0;c/=d;//lu-rv=c
				exgcd(l,r,u,v);//lu-rv=1
				u*=c;v*=c;//lu-rv=c
				ll m=max(max((r-u-1)/r,(l-v-1)/l),0ll);
				u+=r*m,v+=l*m;
				m=max(min(u/r,v/l),0ll);
				u-=r*m;v-=l*m;
				//cout<<u*a+i<<' '<<v*b+j<<endl;
				if(!~ans||ans>v*b+j)ans=v*b+j;
			}if(~ans)cout<<ans<<endl;
			else puts("infinity");
	}
	return 0;
}
