
function makeHist(numbers) {
    const hist = new Map()

    for (const n of numbers) {
        if (hist.has(n)) {
            hist.set(n, hist.get(n) + 1)
        }
        else {
            hist.set(n, 1)
        }
    }

    return hist
}

function main(lines) {
    const [N] = parseNumberLine(lines[0])
    const numbers = parseNumberLine(lines[1])
    const [Q] = parseNumberLine(lines[2])
    const operationLines = lines.slice(3)

    const hist = makeHist(numbers)

    let result = 0
    hist.forEach((v, k) => {
        result += k * v
    })

    for (let i = 0; i < Q; ++i) {
        const [B, C] = parseNumberLine(operationLines[i])

        if (hist.has(B)) {
            const count = hist.get(B)
            hist.delete(B)

            if (hist.has(C)) {
                hist.set(C, hist.get(C) + count)
            }
            else {
                hist.set(C, count)
            }
            result += (C - B) * count
        }

        console.log(result)
    }
}

function parseNumberLine(line, radix = 10) {
    return line.split(/\s+/).map(_ => parseInt(_, radix))
}

main(require("fs").readFileSync("/dev/stdin", "utf8").split("\n"))
