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

import collections
cca = collections.Counter(a)

for i in range(int(input())):
    b,c = map(int,input().split())
    num = cca[b]
    cca[b] = 0
    cca[c] += num 
    
    print(ans - num*(b-c))