n = int(input())
a = list(map(int, input().split()))
q = int(input())
bc = [list(map(int, input().split())) for i in range(q)]

sumA = 0
num = [0]*100000
for aa in a:
    num[aa] += 1
    sumA += aa

for b, c in bc:
    if b < 100001 and c <100001:
        sumA = sumA + ((c - b) * num[b])
        num[c] += num[b]
        num[b] = 0
    print(sumA)
