#pragma GCC optimize("Ofast", "unroll-loops")

#include <bits/stdc++.h>

using namespace std;

using ll = long long;
using ld = long double;

constexpr ll INF = (1LL << 61);

ll extgcd(ll a, ll b, ll& x, ll& y){
    if (b > a) return extgcd(b, a, y, x);
    if (b == 0){
        x = 1; y = 0;
        return a;
    }
    int g = extgcd(b, a % b, y, x);
    y -= (a / b) * x;
    return g;
}

ll gcd(ll a, ll b){
    if (b > a) return gcd(b, a);
    if (b == 0) return a;
    return gcd(b, a % b);
}

ll solve(ll& X, ll& Y, ll& P, ll& Q){
	ll ret = INF;
	ll Z = 2 * (X + Y);
	ll R = P + Q;
	ll _n, _m;
	ll g = extgcd(Z, R, _n, _m);
	Z /= g; R /= g;
	for (int i = -Q + 1; i < Y; ++i){
		if ((X + i + g) % g != P % g) 
			continue;
		ll tmp = (P / g) - ((X + i) / g);
		ll n = _n * tmp;
		ll m = _m * tmp;
		ll t = max(-n / R, m / Z) - 5;
		n += R * t;
		m -= Z * t;
		while (n < 0 || m > 0){
			n += R;
			m -= Z;
		}
		ret = min(ret, 2 * (X + Y) * n + X + max(i, 0));
	}
	return ret;
}

int main(void){
	int T; cin >> T;
	while (T--){
		ll X, Y, P, Q;
		cin >> X >> Y >> P >> Q;
		ll ret = solve(X, Y, P, Q);
		if (ret == INF) cout << "infinity" << endl;
		else cout << ret << endl;
	}
	return 0;
}

#include <set>
#include <map>
#include <iostream>
using namespace std;

long long check(multiset<long long>& M1, multiset<long long>& M2) {
    auto m1 = M1.begin();
    long long ret = 1000000000000000000;
    while (m1 != M1.end()) {
        long long d1 = -1;
        long long d2 = -1;
        
        auto m2 = M2.lower_bound(*m1);
        if (m2 != M2.end()) {
            d1 = abs(*m1 - *m2);
        }
        long long cm2 = *m2;
        while (m2 != M2.begin() && *m2 == cm2) {
            m2--;
            if (d2 == -1 || d2 > abs(*m1 - *m2)) {
                d2 = abs(*m1 - *m2);
            }
        }
        
        if (d1 != -1 && d1 < ret) {
            ret = d1;
        }
        if (d2 != -1 && d2 < ret) {
            ret = d2;
        }

        m1++;
    }
    
    return ret;
    }

int main(void){
    // Your code here!
    long long N;
    multiset<long long> R;
    multiset<long long> G;
    multiset<long long> B;

    cin>> N;
    for (int i = 0; i < 2 * N; i++) {
        long long a;
        char c;
        cin >> a >> c;
        if (c == 'R') {
            R.insert(a);
        } else if  (c == 'G') {
            G.insert(a);
        } else if  (c == 'B') {
            B.insert(a);
        }
    }
    if (R.size() % 2 == 1 && G.size() % 2 == 1) {
        long long ret1 = check(R, G);
        long long ret2 = check(R, B) + check(G, B);
        if (ret1 < ret2) {
            cout << ret1;
        } else {
            cout << ret2;
        }
        return 0;
    }
    if (R.size() % 2 == 1 && B.size() % 2 == 1) {
        long long ret1 = check(R, B);
        long long ret2 = check(R, G) + check(B, G);
        if (ret1 < ret2) {
            cout << ret1;
        } else {
            cout << ret2;
        }
        return 0;
    }
    if (G.size() % 2 == 1 && B.size() % 2 == 1) {
        long long ret1 = check(G, B);
        long long ret2 = check(G, R) + check(B, R);
        if (ret1 < ret2) {
            cout << ret1;
        } else {
            cout << ret2;
        }
        return 0;
    }

    

    cout << 0;
    
}
