#include<iostream>
#include<cstdio>
#include<cstring>
#include<string>
#include<vector>
#include<cmath>
#include<algorithm>
#include<map>
#include<queue>
#include<deque>
#include<iomanip>
#include<tuple>
#include<cassert>
#include<set>
#include<complex>
#include<numeric>
#include<functional>
#include<unordered_map>
#include<unordered_set>
using namespace std;
typedef long long int LL;
typedef pair<int,int> P;
typedef pair<LL,LL> LP;
const int INF=1<<30;
const LL MAX=1e9+7;

void array_show(int *array,int array_n,char middle=' '){
    for(int i=0;i<array_n;i++)printf("%d%c",array[i],(i!=array_n-1?middle:'\n'));
}
void array_show(LL *array,int array_n,char middle=' '){
    for(int i=0;i<array_n;i++)printf("%lld%c",array[i],(i!=array_n-1?middle:'\n'));
}
void array_show(vector<int> &vec_s,int vec_n=-1,char middle=' '){
    if(vec_n==-1)vec_n=vec_s.size();
    for(int i=0;i<vec_n;i++)printf("%d%c",vec_s[i],(i!=vec_n-1?middle:'\n'));
}
void array_show(vector<LL> &vec_s,int vec_n=-1,char middle=' '){
    if(vec_n==-1)vec_n=vec_s.size();
    for(int i=0;i<vec_n;i++)printf("%lld%c",vec_s[i],(i!=vec_n-1?middle:'\n'));
}
template<typename T> ostream& operator<<(ostream& os,const vector<T>& v1){
    int n=v1.size();
    for(int i=0;i<n;i++){
        if(i)os<<" ";
        os<<v1[i];
    }
    return os;
}
template<typename T1,typename T2> ostream& operator<<(ostream& os,const pair<T1,T2>& p){
    os<<p.first<<" "<<p.second;
    return os;
}
template<typename T> istream& operator>>(istream& is,vector<T>& v1){
    int n=v1.size();
    for(int i=0;i<n;i++)is>>v1[i];
    return is;
}

namespace sol{

    int n,m;
    const int N=5,M=3;
    int t[3333][N];

    bool check(int q){
        vector<vector<char>> dp(1<<N,vector<char>(M+10));
        dp[0][0]=1;
        int i,j,k;
        int a,b;
        for(i=0;i<n;i++){
            a=0;
            for(j=0;j<N;j++){
                if(t[i][j]>=q)a^=1<<j;
            }
            for(j=0;j<(1<<N);j++){
                for(k=M-1;k>=0;k--){
                    if(dp[j][k])dp[j|a][k+1]=1;
                }
            }
        }
        return dp[(1<<N)-1][M];
    }

    void solve(){
        int i,j,k;
        int a,b,c;
        int z[3]={0,INF};
        cin>>n;
        for(i=0;i<n;i++){
            for(j=0;j<N;j++){
                cin>>t[i][j];
            }
        }
        while(z[1]-z[0]>1){
            z[2]=(z[0]+z[1])/2;
            if(check(z[2]))z[0]=z[2];
            else z[1]=z[2];
        }
        cout<<z[0]<<endl;
    }
}

int main(){
    sol::solve();
}
#include <bits/stdc++.h>
#define rep(i, a, n) for(int i = a; i < n; i++)
using namespace std;
using ll = long long;
using P = pair<int, int>;
const ll INF = 3e18;

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

// EXTGCD  a*x + b*y = extGCD
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;
}

// Chinese Remainder Theorem
// x ≡ r (mod m)
pair<ll, ll> ChineseRem(ll b1, ll m1, ll b2, ll m2){
    ll p, q;
    ll d = extGCD(m1, m2, p, q); // p is inv of m1/d (mod m2/d)
    if((b2 - b1)%d != 0) return make_pair(0, -1); // no answer
    ll m = m1*(m2/d); // lcm(m1, m2)
    ll tmp = (b2 - b1)/d*p%(m2/d);
    ll r = mod(b1 + m1*tmp, m);
    return make_pair(r, m);
}

void solve(){
    ll x, y, p, q;
    cin >> x >> y >> p >> q;
    ll a = (x + y)*2, b = p + q;
    ll ans = INF;
    rep(i, 0, y){
        rep(j, 0, q){
            pair<ll, ll> P = ChineseRem(x+i, a, p+j, b);
            if(P.first != 0) ans = min(ans, P.first);
        }
    }
    if(ans == INF) cout << "infinity" << endl;
    else cout << ans << endl;
    return;
}

int main()
{
    int t;
    cin >> t;
    while(t--) solve();
    return 0;
}


