n = int(input()) graph = [[] for _ in range(n)] for i in range(n-1): a, b = map(int, input().split()) graph[a].append((i,b)) graph[b].append((i,a)) edge = [0]*(n-1) parent = [0]*n bfs = [0] for i in range(n): u = bfs[i] for e, v in graph[u]: if v == parent[u]: continue parent[v] = u edge[e] = v bfs.append(v) def mul(x, y): a = (x[0]*y[0]+x[1]*y[2])%(10**9+7) b = (x[0]*y[1]+x[1]*y[3])%(10**9+7) c = (x[2]*y[0]+x[3]*y[2])%(10**9+7) d = (x[2]*y[1]+x[3]*y[3])%(10**9+7) return (a,b,c,d) mat = [(1,0,0,1)]*n for _ in range(int(input())): a = input().split() if a[0] == 'x': i, *x = map(int, a[1:]) mat[edge[i]] = x else: i, j = map(int, a[1:]) c = mat[j] while i != parent[j]: j = parent[j] c = mul(mat[j], c) print(*c)