from math import gcd def max_factor(f, x): while x % f == 0: x //= f x = gcd(f, x) if x == 1: return f return max_factor(x, f) def solve(a, b, c): ans = 0 g = gcd(c, a) if g == 1: return 0 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 ea * b // ec % 998244353 for _ in range(int(input())): print(solve(*map(int, input().split())))