import sys
input = sys.stdin.readline

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

dict = {}
for i in range(1, 10**5+1):
    dict[i] = 0
for i in a:
    dict[i] += 1

for i in range(q):
    b, c = map(int, input().split())
    dict[c] += dict[b]
    dict[b] = 0
    ans = 0
    for i, j in dict.items():
        ans += i*j
    print(ans)