import collections

n = int(input())
lis = list(map(int,input().split()))
q = int(input())
total = sum(lis)

nlis = [0] * 10001

for i in lis:
    nlis[i-1] += 1

for i in range(q):
    b, c = map(int,input().split())
    total = total -(b*nlis[b-1]) + (c*nlis[b-1])
    nlis[c-1] += nlis[b-1]
    nlis[b-1] = 0
    print(total)
    
    
