from collections import Counter

n = int(input())
a = list(map(int, input().split()))
q = int(input())
l = [list(map(int, input().split())) for _ in range(q)]

for i in range(q):
    (b, c) = l[i] 
    dic = Counter(a)
    for i in range(dic[b]):
        a.remove(b)
        a.append(c)
    print(sum(a))