n = int(input())
A = list(map(int, input().split()))

count = {}
for a in A:
    count[a] = count.get(a, 0) + 1

q = int(input())
result = []
for i in range(q):
    b, c = list(map(int, input().split()))
    if count.get(b):
        count[c] = count.get(c, 0) + count.pop(b)
    sum = 0
    for key, value in count.items():
        sum += key * value
    result.append(str(sum))
print('\n'.join(result))