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

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

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