#include <iostream>
#include <string>
#include <vector>
#include <array>
#include <stack>
#include <queue>
#include <deque>
#include <algorithm>
#include <set>
#include <map>
#include <unordered_map>
#include <bitset>
#include <cmath>
#include <functional>
#include <cassert>
#include <iomanip>
#include <numeric>
#include <memory>
#include <random>
#define VV(a, b, c, d) vector<vector<d>>(a, vector<d>(b, c))
#define VVV(a, b, c, d, e) vector<vector<vector<e>>>(a, VV(b, c, d, e));
#define all(obj) (obj).begin(), (obj).end()
typedef long long ll;
typedef long double ld;
typedef std::vector<ll> v1;
typedef std::vector<v1> v2;
typedef std::vector<v2> v3;
using namespace std;


long long floor_sum(long long n,long long m,long long a,long long b){
	long long res=0;
	if(a>=m){
		res+=n*(n-1)/2*(a/m);
		a%=m;
	}
	if(b>=m){
		res+=n*(b/m);
		b%=m;
	}

	long long y_max=(a*(n-1)+b)/m;
	if(y_max==0) return res;
	return res+y_max+floor_sum(y_max,a,m,(a*(n-1)+b)-m*y_max);
}

void solve(){
  ll x, y, p, q;
  scanf("%lld %lld %lld %lld", &x, &y, &p, &q);

  ll l = 0, r = 1000000000;
  if(floor_sum(r, p+q, 2*(x+y), x+y+q-1) == floor_sum(r, p+q, 2*(x+y), x)){
    std::cout << "infinity" << '\n';
    return;
  }

  while(r-l>1){
    ll mid = (l+r)/2;
    if(floor_sum(mid, p+q, 2*(x+y), x+y+q-1) == floor_sum(mid, p+q, 2*(x+y), x)) l = mid;
    else r = mid;
  }

  ll distsum = 2*(r-1)*(x+y) + x;
  if(distsum % (p+q) >= p) printf("%lld\n", distsum);
  else printf("%lld\n", distsum + (p - distsum % (p+q)));
}

int main(){
  int t;
  scanf("%d\n", &t);
  for(int i=0;i<t;i++) solve();
}

#include <bits/stdc++.h>
using namespace std;
/* * * *   * * *   	* * * */
typedef long long ll;
typedef long double ld;
typedef pair<int, int> pii;
/* * * *   * * *   	* * * */
/* *
 *
 * I love Ylva Eriksson
 *
 * */
const ll mod = 1e9 + 7;
ll add(ll a, ll b) { return a + b > mod ? a + b - mod : a + b; }
ll mult(ll a, ll b) { return a * b % mod; }
ll powmod(ll b, ll e) {
	if (e == 0) return 1;
	ll u = powmod(b, e / 2);
	u = mult(u, u);
	if (e & 1) u = mult(u, b);
	return u;
}
const int maxn = 1010;
ll f[maxn];
int main(){
	ios_base::sync_with_stdio(0), cin.tie(0), cout.tie(0);
	//freopen("input.txt", "r", stdin);
	//freopen("output.txt", "w", stdout);
	f[1] = f[0] = 1;
	for (int i = 2; i < maxn; ++i) f[i] = add(f[i - 1], f[i - 2]);
	int n, op[2][2]; cin >> n;
	char ci;
	cin >> ci; op[0][0] = ci - 'A';
	cin >> ci; op[0][1] = ci - 'A';
	cin >> ci; op[1][0] = ci - 'A';
	cin >> ci; op[1][1] = ci - 'A';
	if (n <= 3) { cout << 1 << endl; return 0; }
	if (op[0][1]) { //ABB
		if (op[1][1]) cout << 1 << endl;
		else cout << (op[1][0] == 0 ? powmod(2, n - 3) : f[n - 2]) << endl;
	} else { //AAB
		if(!op[0][0]) cout << 1 << endl;
		else cout << (op[1][0] ? powmod(2, n - 3) : f[n - 2]) << endl;
	}
	return 0;
}