#include<bits/stdc++.h>
#define P 998244353
#define int long long
using namespace std;
int f[10010], g[10010], ans = 1;
int mod(int a) //a%b
{
a %= P;
return a;
}
int mod2(int a)
{
a %= P;
if (a < 0)
return a + P;
return a;
}
int mi(int a, int b) //a^b
{
mod(a);
int sum = 1;
for (int i = b, j = a; i; i = i >> 1, j = j * j % P)
{
if (i & 1)
sum = sum * j % P;
}
return sum;
}
signed main()
{
int t;
cin >> t;
for (int i = 0; i < t; i++)
{
int n, A, B, X, Y, cnt_f = 2, cnt_g = 2;
scanf("%lld %lld %lld %lld %lld", &n, &A, &B, &X, &Y);
// fill(f, f + 10010 + 1, 0);
// fill(g, g + 10010 + 1, 0);
// fill(ans,ans+100,0);
ans = 1;
f[1] = A, f[2] = B;
g[1] = X, g[2] = Y;
for (int j = 3; j <= n; j++)
{
int tmp = (int)sqrtl(f[j - 1] * f[j - 2]) + 1;cnt_f = j;f[j] = tmp;
if (tmp == f[j - 1])
break;
}
for (int j = 3; j <= n; j++)
{
int tmp = (int)sqrtl(g[j - 1] * g[j - 2]) + 1; g[j] = tmp;cnt_g = j;
if (tmp == g[j - 1])
break;
}
// for (int j = 1; j <= cnt_f; j++)
// {
// cout << f[j] << " ";
// }
// cout << endl;
// for (int j = 1; j <= cnt_g; j++)
// {
// cout << g[j] << " ";
// }
// cout << endl;
for (int j = cnt_f + 1; j <= cnt_g; j++)
{
f[j] = (int)sqrtl(f[j - 1] * f[j - 2]) + 1;
cnt_f = j;
// cout<<f[j]<<endl;
}
for (int j = cnt_g + 1; j <= cnt_f; j++)
{
g[j] = (int)sqrtl(g[j - 1] * g[j - 2]) + 1;
cnt_g = j;
// cout<<g[j]<<endl;
}
// for (int j = 1; j <= cnt_f; j++)
// {
// cout << f[j] << " ";
// }
// cout << endl;
// for (int j = 1; j <= cnt_g; j++)
// {
// cout << g[j] << " ";
// }
int tmp = max(cnt_f, cnt_g);
if (n <= tmp)
{
for (int j = 1; j <= n; j++)
ans = 1LL * ans * (g[j] - f[j]) % P, ans %= P;
printf("%lld\n", mod2(ans));
continue;
}
// tmp = max(cnt_f, cnt_g);
// cout << ans << " " << cnt_f << " " << cnt_g << " ";
for (int j = 1; j <= tmp; j++)
{
ans *= (g[j] - f[j]) % P;
ans %= P;
// cout << ans << " ";
}
ans *= mi(f[cnt_f - 1] - g[cnt_g - 1], (n - cnt_f + 1) >> 1);
ans = mod(ans);
// cout << tmp << " " << ans << " ";
ans *= mi(f[cnt_f] - g[cnt_g], (n - cnt_f + 0) >> 1);
printf("%lld\n", mod2(ans));
}
return 0;
}
/*
4 5 5 5 5 5 5 5
1 4 3 4 4 4 4 4
*/