N = int(input())
A = list(map(int, input().split()))
Q = int(input())

numlist = [0] * 10**5
for x in A:
    numlist[x] +=1


for i in range(Q):
    ans=0
    B, C = list(map(int, input().split()))

    numlist[C] += numlist[B]
    numlist[B] = 0

    for j in range(10**5):
        ans = numlist[j] * j + ans

    print(ans)
