from math import gcd def extract(a, x): if a == 0: return x a = gcd(a, x) x //= a while True: g = gcd(a, x) if g == 1: return a x //= g a *= g def max_factor(f, x): while x % f == 0: x //= f x = extract(f, x) if x == 1: return f return max_factor(x, f) def solve(a, b, c): ans = 0 while a % c == 0: a //= c ans += b g = gcd(c, a) if g == 1: return ans f = max_factor(g, c) ea = ec = 0 while a % f == 0: a //= f ea += 1 while c % f == 0: c //= f ec += 1 return ans + ea * b // ec for _ in range(int(input())): print(solve(*map(int, input().split())) % 998244353)