from collections import Counter 

n = int(input())
a = list(map(int, input().split()))
co = Counter(a)
q = int(input())
for _ in range(q):
    b, c = map(int, input().split())
    if b in co.keys():
        co[c] += co[b]
        co[b] = 0
    ans = 0
    for i in co.items():
        ans += i[0] * i[1]
    print(ans)