import sys sys.setrecursionlimit(10000000) rr = sys.stdin.readline def dfs_sub(N, ch, sub_size, sub_dist, x): chx = ch[x] if len(chx) == 0: sub_size[x] = 1 sub_dist[x] = 0 return total_size = 1 total_dist = 0 for c in chx: dfs_sub(N, ch, sub_size, sub_dist, c) c_size = sub_size[c] total_size += c_size total_dist += c_size + sub_dist[c] sub_size[x] = total_size sub_dist[x] = total_dist def dfs_total(N, ch, sub_size, sub_dist, total_dist, parent_to_this, x): v = parent_to_this + sub_dist[x] total_dist[x] = v for c in ch[x]: to_child = (v - sub_dist[c] - sub_size[c]) + (N - sub_size[c]) dfs_total(N, ch, sub_size, sub_dist, total_dist, to_child, c) def read_tree(N): P = [int(x)-1 for x in rr().split()] ch = [[] for _ in range(N)] for i in range(N-1): ch[P[i]].append(i) sub_size = [0] * N sub_dist = [0] * N dfs_sub(N, ch, sub_size, sub_dist, N-1) total_dist = [0] * N dfs_total(N, ch, sub_size, sub_dist, total_dist, 0, N-1) return total_dist def solve(): W, E, C = map(int, rr().split()) tree_w = read_tree(W) tree_e = read_tree(E) tree_w_sum = sum(tree_w) // 2 tree_e_sum = sum(tree_e) // 2 count_internal = (W*(W-1) + E*(E-1)) // 2 count = count_internal + W*E A = [] for _ in range(C): a, b = map(int, rr().split()) a_d, b_d = tree_w[a-1], tree_e[b-1] v = tree_w_sum + tree_e_sum + a_d * E + b_d * W + W*E A.append(v / count) return A for t in range(1, int(rr())+1): print(f"Case #{t}:", " ".join(str(x) for x in solve()))