#include<bits/stdc++.h>
using namespace std;
using u64 = unsigned long long;
using u32 = unsigned int;
using i64 = long long;
mt19937_64 rnd(random_device{}());
const int mod = 998244353;
const int N = 3e5 + 5;
const int M = 2e6 + 5;
int n, m, s, t;
i64 fastpow(i64 a, int n) {
i64 res = 1;
a %= mod;
while(n) {
if(n & 1) res = res * a % mod;
a = a * a % mod;
n >>= 1;
}
return res;
}
i64 rev(i64 a) {
return fastpow(a, mod - 2);
}
void RuinGuard(int tc)
{
int x, y;
cin >> x >> y;
int a, b, c;
cin >> a >> b >> c;
int pa = a * rev(a + b) % mod, pb = b * rev(a + b) % mod;
auto f = [&](int x, int y, int pa, int pb) -> int {
if(!x) return 0;
return fastpow(pa, y / x) * (1 - f(y % x, x, b, a) + mod) % mod;
};
return cout << f(x, y, pa, pb) << '\n';
}
signed main()
{
ios::sync_with_stdio(0), cin.tie(0), cout.tie(0);
int testcase = 1;
cin >> testcase;
for(int tc = 1; tc <= testcase; tc++) {
RuinGuard(tc);
}
return 0;
}