AtCoder Beginner Contest 171
collections.defauldict(int)
を使って、各数字の個数を数え上げておく
import collections
N = int(input())
A = sorted(map(int, input().split()))
cnt = collections.defaultdict(int)
for a in A:
cnt[a] += 1
ans = sum(A)
Q = int(input())
for _ in range(Q):
B, C = map(int, input().split())
ans += (C - B) * cnt[B]
cnt[C] += cnt[B]
cnt[B] = 0
以降追加予定