_ = Int(readLine()!)!
let AN = readLine()!.split(separator: " ").compactMap { Int($0) }
let Q = Int(readLine()!)!
let BCQ: [(Int, Int)] = (1...Q).map { _ in
    let bc = readLine()!.split(separator: " ").compactMap { Int($0) }
    return (bc[0], bc[1])
}

var dict = [Int: Int]()
for an in AN {
    if dict[an] == nil {
        dict[an] = 1
    } else {
        dict[an]! += 1
    }
}

var sum = dict.reduce(0) { $0 + ($1.key * $1.value) }
for bc in BCQ {
    if dict[bc.0] != nil {
        let diff = (bc.1 - bc.0) * dict[bc.0]!
        sum += diff

        if dict[bc.1] != nil {
            dict[bc.1]! += dict[bc.0]!
        } else {
            dict[bc.1] = dict[bc.0]
        }
        dict[bc.0] = 0
    }
    print(sum)
}
