import sys input = sys.stdin.readline def prime_factors(m: int): pf = [] if (m & 1) == 0: pf.append(2) while (m & 1) == 0: m >>= 1 p = 3 while p * p <= m: if m % p == 0: pf.append(p) while m % p == 0: m //= p p += 2 if m != 1: pf.append(m) return pf P = 998244353 t, m = map(int, input().split()) pf = prime_factors(m) k = len(pf) for _ in range(t): n = int(input()) a = list(map(int, input().split())) w = list(map(int, input().split())) a2 = [] w2 = [] for i, (ai, wi) in enumerate(zip(a, w)): if m % ai == 0: a2.append(ai) w2.append(wi) a = a2 w = w2 zeta = [1] * (1 << k) for ai, wi in zip(a, w): t = 0 for j, p in enumerate(pf): if (m // p) % ai: t |= 1 << j zeta[t] = zeta[t] * ((1 + wi) % P) % P block = 1 while block < 1 << k: offset = 0 while offset < 1 << k: for i in range(offset, offset + block): zeta[i + block] = zeta[i + block] * zeta[i] % P offset += 2 * block block <<= 1 ans = 0 for s in range(1 << k): if (bin(s).count('1') & 1): ans -= zeta[s] else: ans += zeta[s] if m == 1: ans -= 1 print(ans % P, flush=False)