using DataStructures
parseline(str) = parse.(Int, split(str))
function Replacing()
    data = readlines()
    A = parseline(data[2])
    nums = zeros(Int, 10^5)
    ans = 0
    for a in A
        ans += a
        nums[a] += 1
    end
    BC = parseline.(data[4:end])
    ansStr = ""
    for bc in BC
        ans += (bc[2] - bc[1]) * nums[bc[1]]
        nums[bc[2]] += nums[bc[1]]
        nums[bc[1]] = 0
        ansStr *= "$ans\n"
    end
    println(ansStr)
end
Replacing()