func readInt() -> Int {
    return Int(readLine()!)!
}

func readTwoInts(separator: String.Element = " ") -> (a: Int, b: Int) {
    let ints = readLine()!.split(separator: separator).map { Int($0)! }
    return (a: ints[0], b: ints[1])
}

func readInts(separator: String.Element = " ") -> [Int] {
    return readLine()!.split(separator: separator).map { Int($0)! }
}

// 普通にやるとTLE
// どの要素がどれだけあるかを持っておいて、差分更新をする
func mainD() {
    let N = readInt()
    var numbers = readInts()
    let Q = readInt()
    let pairs = (0..<Q).map { _ in readTwoInts() }

    var counts: [Int] = .init(repeating: 0, count: 1_000_00 + 1)

    for number in numbers {
        counts[number] += 1
    }

    var sum = 0
    for pair in pairs {
        let changed = counts[pair.a]
        counts[pair.b] += counts[pair.a]
        counts[pair.a] = 0

        if sum == 0 {
            sum = counts.enumerated().reduce(0) { $0 + ($1.offset * $1.element) }
            print(sum)
        } else {
            sum += (pair.b - pair.a) * changed
            print(sum)
        }
    }

//    var sums: [Int] = .init(repeating: 0, count: Q)
//    var index = 0
//    for pair in pairs {
//        numbers = numbers.map { number in
//            let newNumber = number == pair.a ? pair.b : number
//            sums[index] += newNumber
//            return newNumber
//        }
//
//        index += 1
//    }
//
//    for sum in sums {
//        print(sum)
//    }
}
mainD()
