from sys import stdin def f(x, a): return (x + a - 1) // a * a def g(a, b): if b == 0: return a return g(b, a % b) def solve(): x, a, b = map(int, stdin.readline().split()) if a > b: a, b = b, a if b == 1: print(1) return if a == 1 or a == b: if x == f(x, b): print(1) else: print(2) return k = f(f(x, b), a) x1, x2, x3 = f(x, a), f(x, b), f(f(x, a), b) ch = set() if x < k: ch.add(x) if x1 < k: ch.add(x1) if x2 < k: ch.add(x2) if x3 < k: ch.add(x3) x = k gcc = g(a, b) x, a, b = x // gcc, a // gcc, b // gcc ans = len(ch) if x % b == 0: ans += 1 else: ans += 2 * (f(x // a, b) // b * a - f(x, b) // b + 1) print(ans % 998244353) for _ in range(int(input())): solve()