using DataStructures
parseline(str) = parse.(Int, split(str))
function Replacing()
    data = readlines()
    N = parse(Int, data[1])
    A = parseline(data[2])
    nums = zeros(Int, 10^5)
    for a in A
        nums[a] += 1
    end
    Q = parse(Int, data[3])
    BC = parseline.(data[4:end])
    ans = sum(A)
    ansStr = ""
    for bc in BC
        nums[bc[1]] == 0 && continue
        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()