import sys from functools import lru_cache sys.setrecursionlimit(10**7) def I(): return int(sys.stdin.readline().rstrip()) def MI(): return map(int,sys.stdin.readline().rstrip().split()) def LI(): return list(map(int,sys.stdin.readline().rstrip().split())) def LI2(): return list(map(int,sys.stdin.readline().rstrip())) def S(): return sys.stdin.readline().rstrip() def LS(): return list(sys.stdin.readline().rstrip().split()) def LS2(): return list(sys.stdin.readline().rstrip()) N = I() TAB = [(0,0,0)]*2+[tuple(MI()) for _ in range(N-1)] Q = I() X = LI() mod = 998244353 @lru_cache(maxsize=1000) def f(i,x): if i == 0: return 1 if i == 1: return x t,a,b = TAB[i] if t == 1: res = f(a,x)+f(b,x) elif t == 2: res = a*f(b,x) else: res = f(a,x)*f(b,x) return res % mod for x in X: print(f(N,x))