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

dic = {}
for i in a:
    if i not in dic:
        dic[i] = 1
    else:
        dic[i] += 1

sum = 0
for k in dic:
    v = dic[k]
    sum += k*v

for i in bc:
    if i[0] in dic:
        if i[1] in dic:
            dic[i[1]] += dic[i[0]]
        else:
            dic[i[1]] = dic[i[0]]
        o = (i[1]- i[0])*dic[i[0]]
        dic[i[0]] = 0
        sum += o
        print(sum)
