n = int(input())
a = list(map(int,input().split()))
q = int(input())

from collections import Counter

counter = Counter(a)
ans = sum(a)

for _ in range(q):
    before, after = list(map(int,input().split()))

    ans -= before * counter[before]
    ans += after * counter[after] 
    
    counter[after] += counter[before] 
    counter[before] -= 0

    print(ans)