func readIntArray() -> [Int] {
    readLine()!.split(separator: " ").map({Int($0)!})
}

func readIntPair() -> (Int, Int) {
    let line = readLine()!.split(separator: " ").map({Int($0)!})
    return (line[0], line[1])
}

let N = Int(readLine()!)!
let A = readIntArray()
let Q = Int(readLine()!)!

var count = Array(repeating: 0, count: 100001)
var sum = 0
A.forEach({
    count[$0] += 1
    sum += $0
})
var ans: [Int] = []
(1...Q).forEach({ _ in
    let (B, C) = readIntPair()
    sum += (C - B) * count[B]
    count[C] += count[B]
    count[B] = 0
    ans.append(sum)
})

ans.forEach({
    print($0)
})
