import sys input = sys.stdin.read from collections import defaultdict, deque def main(): data = input().split() index = 0 n = int(data[index]) index += 1 tree = defaultdict(list) for _ in range(n - 1): u = int(data[index]) - 1 v = int(data[index + 1]) - 1 tree[u].append(v) tree[v].append(u) index += 2 m = int(data[index]) index += 1 routes = [] for _ in range(m): s = int(data[index]) - 1 t = int(data[index + 1]) - 1 routes.append((s, t)) index += 2 # 村を通過する行商人の数をカウントする village_count = [0] * n def bfs_count_passes(start, end): queue = deque([start]) visited = [False] * n visited[start] = True parents = [-1] * n # BFS to find the path from start to end while queue: node = queue.popleft() if node == end: break for neighbor in tree[node]: if not visited[neighbor]: visited[neighbor] = True parents[neighbor] = node queue.append(neighbor) # Backtrack to count the path node = end while node != -1: village_count[node] += 1 node = parents[node] # 各ルートについてBFSを実行 for s, t in routes: bfs_count_passes(s, t) # 税額の総計を計算 total_tax = 0 for i in range(n): if village_count[i] > 0: total_tax += sum(range(village_count[i] + 1)) print(total_tax) if __name__ == "__main__": main()