import numpy as np 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)) parents = [0]*n edges = [0]*(n-1) def dfs(u, p): parents[u] = p for i, v in graph[u]: if v == p: continue edges[i] = v dfs(v, u) dfs(0, 0) mats = [np.array([[1,0],[0,1]])]*n for _ in range(int(input())): a = input().split() if a[0] == 'x': i, a, b, c, d = map(int, a[1:]) x = np.array([[a,b],[c,d]]) mats[edges[i]] = x else: i, j = map(int, a[1:]) c = mats[j] while i != parents[j]: j = parents[j] c = np.dot(mats[j], c) print(*(e for r in c for e in r))