N = int(input()) G = [[] for _ in range(N)] for _ in range(N- 1): u,v = map(int,input().split()) u -= 1 v -= 1 G[u].append(v) G[v].append(u) N = int(input()) G = [[] for _ in range(N)] for _ in range(N- 1): u,v = map(int,input().split()) u -= 1 v -= 1 G[u].append(v) G[v].append(u) dp = [[0] * 2 for _ in range(N)] stack = [(0,-1),(~0,-1)] ans = 0 while stack: now,p = stack.pop() if now < 0: now = ~now for v in G[now]: if v != p: stack.append((v,now)) stack.append((~v,now)) else: t1 = 0 tt = 0 t2 = 0 for v in G[now]: if v != p: tt += 1 dp[now][0] += 1 dp[now][1] += dp[v][0] t1 = dp[v][0] t2 = dp[v][1] ans += 1 + dp[v][0] + dp[v][1] tmp = 0 for v in G[now]: if v != p: tmp += (t1 - dp[v][0]) ans += tmp + (tt - 1) ** tt // 2 print(ans)