#!/usr/bin/python2 # -*- coding: utf-8 -*- # † mod = 998244353 n = int(raw_input()) T = [None] * (n+1) A = [None] * (n+1) B = [None] * (n+1) for i in xrange(2, n+1): T[i], A[i], B[i] = map(int, raw_input().split()) def f(x): # dp[i] := fi(x) dp = [0] * (n+2) dp[0] = 1 dp[1] = x for i in xrange(2, n+1): t, a, b = T[i], A[i], B[i] if t == 1: dp[i] = dp[a] + dp[b] dp[i] %= mod elif t == 2: dp[i] = a * dp[b] dp[i] %= mod elif t == 3: dp[i] = dp[a] * dp[b] dp[i] %= mod return dp[n] q = int(raw_input()) xs = map(int, raw_input().split()) for x in xs: res = f(x) print res