def dfs(u, adj, dist): for v in adj[u]: if dist[v] == -1: dist[v] = dist[u] + 1 dfs(v, adj, dist) def solve_case(w, e, c, west, east, options): # Build adjacency lists for the existing connections west_adj = defaultdict(list) for i in range(w-1): west_adj[i+1].append(west[i]) west_adj[west[i]].append(i+1) east_adj = defaultdict(list) for i in range(e-1): east_adj[i+1].append(east[i]) east_adj[east[i]].append(i+1) # Calculate the distances between all pairs of stations west_dist = [-1] * w west_dist[0] = 0 dfs(0, west_adj, west_dist) east_dist = [-1] * e east_dist[0] = 0 dfs(0, east_adj, east_dist) total_dist = 0 for i in range(w): for j in range(e): total_dist += west_dist[i] + east_dist[j] # Calculate the average distance for each option results = [] for i in range(c): # Add the new overpass connection to the adjacency lists west_adj[options[i][0]].append(w + i) west_adj[w + i].append(options[i][0]) east_adj[options[i][1]].append(e + i) east_adj[e + i].append(options[i][1]) # Calculate the new distances between all pairs of stations new_west_dist = [-1] * (w + 1) new_west_dist[0] = 0 dfs(0, west_adj, new_west_dist) new_east_dist = [-1] * (e + 1) new_east_dist[0] = 0 dfs(0, east_adj, new_east_dist) new_total_dist = 0 for j in range(w+1): for k in range(e+1): new_total_dist += new_west_dist[j] + new_east_dist[k] # Calculate the average distance with the new overpass connection avg_dist = new_total_dist / ((w+1) * (e+1)) results.append(avg_dist) # Remove the new overpass connection from the adjacency lists west_adj[options[i][0]].remove(w + i) west_adj[w + i].remove(options[i][0]) east_adj[options[i][1]].remove(e + i) east_adj[e + i].remove(options[i][1]) return results # Read input t = int(input()) for case in range(1, t+1): w, e, c = map(int, input().split()) west = list(map(int, input().split())) east = list(map(int, input().split())) options = [list(map(int, input().split())) for _ in range(c)] # Solve case and print output results = solve_case(w, e, c, west, east, options) output_str = "Case #{}: {}".format(case, " ".join("{:.6f}".format(x) for x in results)) print(output_str)