import sys
import collections
input = sys.stdin.readline

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

count = collections.Counter(a)

for i in range(q):
    b,c = map(int, input().split())
    count[c] += count[b]
    count[b] = 0

    sum = 0
    for i in count:
        sum += count[i] * i

    print(sum)