import collections
N = int(input())
A = list(map(int, input().split()))
Q = int(input())
LIST = [0]*(10000+1)
for i in A:
    LIST[i]+=1

sum = sum(A)
for _ in range(Q):
    b, c = map(int, input().split())
    LIST[c]+=LIST[b]
    sum+=LIST[b]*(c-b)
    LIST[b]= 0
    print(sum)
