
function solve()
    N = parse(Int, readline())
    A = parse.(Int, split(readline()))
    Q = parse(Int, readline())
    B = zeros(Int, Q)
    C = zeros(Int, Q)

    for i = 1:Q
        B[i], C[i] = parse.(Int, split(readline()))
    end

    d = Dict{Int, Int}()
    for a in A
        if haskey(d, a) d[a]+=1
        else d[a] = 1 end
    end

    for i = 1:Q
        if !haskey(d, B[i]) continue end
        if haskey(d, C[i])
            d[C[i]] += d[B[i]]
            d[B[i]] = 0
        else
            d[C[i]] = d[B[i]]
            d[B[i]] = 0
        end

        println(sum([d[k]*k for k in keys(d)]))
    end
end

solve()